tags:

views:

29

answers:

1

Hi,

I have data in table a that i want to quench and create into another table b. Wondering how to do this. I was thinking of doing nested CASE expressions. But is this do-able?

For eg:-

Table a:-

S En Eg

-0.2 7888 99 90 9000 788 100 999 888

I want to create another table b, that does this:-

select CASE WHEN S < 0 then (S+1/En-Eg)
ELSE (S-1/En-Eg)) END AS Z from a

I also want to compare Z with other values:-

If z > 0 then 'Good' else 'Bad'

Something like that, can i do this inside table b as well?

+2  A: 

You could "reuse" the CASE expression if you performed it in a subquery:

SELECT z, CASE WHEN z > 0 THEN 'Good' ELSE 'Bad' END AS zdesc
FROM (
    SELECT CASE WHEN S < 0 
        THEN (S + 1 / En - Eg)
        ELSE (S - 1 / En - Eg) END AS z 
    FROM a
) b
Matt Hamilton
thank you matt! that works well :)
rayhan