Link to home
Start Free TrialLog in
Avatar of jgordin
jgordin

asked on

SQL select syntax with embedded logic

i need to select from two tables. both tables can be joined on id (t1.id  = t2.id)

select
              if t2.columnA = 1 then (
                                                       1 as abc-column-name1
                                                        t2.columnX1 as abc-column1
                                                  )
 
               if t2.columnA = 2 then (
                                                         2 as abc-column-name2
                                                         t2.columnX2 as abc-column2
                                                    )
               t1.columnA,
               t2.columnB
from table1 t1, table2 t2
where t1.id = t2.id
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In your example, you attempt to create columns with variable names. SQL isn't meant for that and only allows it in cross-tabs (this doesn't fit your scenario, however).

Instead, manipulate only your data, for example as Patrick showed above, into fixed column names. You can then rename or hide/show these columns further down in the interface. But not directly in the query.

(°v°)