[英]SQL Query Microsoft Azure Case Expression not working with new created column names/variables
[英]Case Expression on a created column using Microsoft SQL
在一個視圖中,我將一個case中的一條select語句放到一起,然后將其作為一列。 列名稱為“ 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
您可以使用子查詢或CTE。 但是SQL Server中另一個有趣的方法是使用outer apply
:
select v.IR2,
(case IR2 when 'Released' then '' else '*' end) as ir
from . . . outer apply
(values (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)
) v(IR2)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.