我有SELECT这些表达式的Postgres 语句:
,CASE WHEN (rtp.team_id = rtp.sub_team_id)
THEN 'testing'
ELSE TRIM(rtd2.team_name)
END AS testing_testing
,CASE WHEN (rtp.team_id = rtp.sub_team_id)
THEN 'test example'
ELSE TRIM(rtd2.normal_data)
END AS test_response
,CASE WHEN (rtp.team_id = rtp.sub_team_id)
THEN 'test example #2'
ELSE TRIM(rtd2.normal_data_2)
END AS another_example
Run Code Online (Sandbox Code Playgroud)
在我的特定查询中有5个字段,其输出取决于是否rtp.team_id = rtp.sub_team_id计算为true.我CASE一遍又一遍地重复具有相同条件的陈述.
有没有什么方法可以组合这些CASE表达式来一次切换多列的输出?
我试图从单个CASE语句中获取多个列(在这种情况下为insuredcode,insuredname).
已尝试以下查询,但它将insuredcode和insuredname连接为一列.
从这样的CASE语句中返回两列的正确语法是什么?
select
case
when a.policyno[2] in ('E', 'W') then c.insuredcode || c.insuredname
else b.insuredcode || b.insuredname
end
from prpcmain a
left join prpcinsured_1 b on b.proposalno=a.proposalno
left join prpcinsured_2 c on c.proposalno=a.proposalno
where a.policyno in (select policyno from policyno_t);
Run Code Online (Sandbox Code Playgroud)