PostgreSQL中SELECT中的CASE语句

时间:2018-01-23 10:01:08

标签: postgresql

我有以下声明:

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

如何在选择陈述中使用'结果'?

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

此代码不起作用

1 个答案:

答案 0 :(得分:0)

您无法在定义它的同一级别使用列别名。您必须使用派生表:

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 

其中re