1

I've the following statement:

SELECT
    CASE 
       WHEN account_id IS NOT NULL 
          THEN value 
          ELSE value_2 
    END AS result

How can I use 'result' in select statment?

SELECT
    CASE 
       WHEN account_id IS NOT NULL 
          THEN value 
          ELSE value_2 
    END AS result,
    result as result_2

This code doesn't work

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kirill Rodeonov
  • 135
  • 1
  • 2
  • 9

1 Answers1

0

You can't use a column alias on the same level where you define it. You have to use a derived table:

select result, 
       result as result_2, 
       ... other columns ...
from (
  select CASE WHEN account_id IS NOT NULL THEN value ELSE value_2 END AS result, 
         .... other columns ...
  from ...
  where ...
) t 

where re