在一个视图中,我将一个select语句放在一个case中,并将其作为一个列进行delcared。列名是' IR2'
如何排除专栏' IR2'?
我最终收到错误消息,其中包含“无效的列名称' IR2'。
我的选择是什么?
case when r.ana = 'nh3' and r.serv='energy' and exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end as IR2,
CASE IR2 WHEN 'Released' then
''
ELSE
'*'
END AS IR
答案 0 :(得分:2)
您可以使用子查询或CTE。但SQL Server中另一种有趣的方法是使用Pandas
:
outer apply
答案 1 :(得分:1)
CTE将是最佳选择。如果要继续使用当前语句,则需要将case语句的副本放在其他case语句中。非常混乱的代码。
SELECT
case when r.ana = 'nh3' and r.serv='energy' and
exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end as IR2,
CASE
(case when r.ana = 'nh3' and r.serv='energy'
and exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end)
WHEN 'Released' then
''
ELSE
'*'
END AS IR