tags:

views:

174

answers:

1

Hi,

I have a sub query that returns one column, showing as GroupType, I then want to do a CASE function on this result within the main query, however I get an invalid column name when using the CASE statement.

Can i do this in SQL to do I have to refer to it by a different name

A: 
SELECT  CASE
        WHEN
        (
        SELECT  column
        FROM    othertable
        ) = 1
        THEN '1'
        ELSE '2'
        END
FROM    mytable

To reuse the subquery result:

SELECT  subvalue, CASE subvalue WHEN 1 THEN 1 ELSE 2 END
FROM    (
        SELECT  (
                SELECT  column
                FROM    othertable
                ) AS subvalue
        FROM    mytable
        ) q
Quassnoi
the problem is, I need to use it twice, once to group by the result, and once to change the output based on one group. I was hoping not to have to type the sub query twice.
Audioillity
Could you please post your query as it is now?
Quassnoi