How to Implement

  • Hi Team,

    I want the sample code to join the Table based on case Statement how to do it.

    Could you Please help me on this.

     

    Select a.*, b.* from A join b

    on a.id = b. id

    Where

    Case ( x=0)

    Join with C Table

    Case (x = 1)

    b.id in (1,,2,3)

    case (x= 2)

    Left join with C

     

     

     

     

    Thanks,

    Rajesh.Alahari

  • Is x a column in table A?  It seems so.  The SQL language does not support dynamic join types or JOIN conditions derived within a WHERE clause based on CASE expressions.  In terms of order of evaluation the FROM clause is evaluated before the WHERE clause.  Maybe you could achieve the intended results by writing separate queries for each case

    select a.*, b.*, c.col1 
    from table_a as a
    join table_b as b on a.id = b.id
    join table_c as c on a.id = c.id
    where a.x = 0

    union all

    select a.*, b.*, null
    from table_a as a
    join table_b as b on a.id = b.id
    where a.x = 1
    and b.id in (1,2,3)

    union all

    select a.*, b.*, c.col1
    from table_a as a
    join table_b as b on a.id = b.id
    left join table_c as c on a.id = c.id
    where a.x = 2;

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Thanks a lot!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply