Joining Tables at a time only one column should Come as Output in a Single Query

Answered Joining Tables at a time only one column should Come as Output in a Single Query

  • Thursday, August 09, 2012 9:42 AM
     
     

    Hi

    i have joined around 7 tables, based on conditions at a time only one column should exist in a SINGLE QUERY, suppose

    For Example :-

    i have a columns of A,B,C,D,E,F,G,H in a table after JOINING some tables, i want 'F' column to be insert some value based on requirement, that Time 'G' Column is not required n should not be their.

    sometimes i want 'G' column, that time 'F' Column is not required n should be not their, This should be achieved using SINGLE QUERY. 

    how to achieve This type of Scenarios ??????????

    Thanks

    Supreeth

All Replies

  • Thursday, August 09, 2012 9:48 AM
     
     
    You can use CASE for this....Use the CASE with the corresponding expression and return the corresponding column for every expression within the case..

    Murali Krishnan

  • Thursday, August 09, 2012 9:51 AM
     
     

    Hi Supreeth,

    Say if u have Query is Select A,B,C,D,E,F,G From table Where <Conditions>,so What u can do is make an Inline query something like

    Select A,B,C..,Case When <Condition> Then G When <Condition> Then F End From (Select A,B,C,D,E,F,G From Table where <Filter conditions> ) As SubTable. This may help u out with ur requirement.


    Please have look on the comment

  • Thursday, August 09, 2012 10:05 AM
     
     

    Santhosh ,

    That A,B,C,D,E,F,G,H columns are output After Joining Several Tables, In place of Coming output like that, i want the output lik this A,B,C,D,E,F(when 'F' Requirement), sometimes i want output like this A,B,C,D,E,G(When 'G' Requirement)

    select A,B,C,D,E,F,G,H

    from <Table>

    Inner Jioin <Table>

    On<Condition>

    Inner Join

    On

    etc.,

  • Thursday, August 09, 2012 10:15 AM
     
     
    Supreeth we need sample data to help you better...

    Murali Krishnan


    • Edited by Murali_CHN Thursday, August 09, 2012 10:16 AM
    •  
  • Thursday, August 09, 2012 10:36 AM
     
     

    Supreeth,

    Though u r fetching the results  lik this A,B,C,D,E,F(when 'F' Requirement), sometimes i want output like this A,B,C,D,E,G(When 'G' Requirement), so at a time u need either of one column value based on requirement right. so why don't u try fetching the column by using case statement.

    If this is really not helping u out just give a model data so that we can wrk it on it and then try to find a proper solution.


    Please have look on the comment

  • Thursday, August 09, 2012 11:07 AM
     
     

     Murali and Santhosh

    i have to join from several tables in that one table say 'Test' contains

    columns 'file_Key' and 'Table_Key' based on filename say 'ABC.txt' that file _key is generated. and based on table_name say 'Emp' table_key is generated
    when i select those columns File_key and Table_Key. if that 'Test' table
    contains both file_key and Table_key then
    it should select both columns , if that 'Test' table contains only File
    Information, then it should display only 'File_key',same ike if that 'Test' table contains Table Information, then it should display Table_key as a output.

  • Thursday, August 09, 2012 1:15 PM
     
     Answered
    I have done it Using Coalesce Function.
    • Marked As Answer by Supreeth TR Thursday, August 09, 2012 1:16 PM
    •