我正在尝试将计算列添加到表中。目前,我有以下 CASE 语句在 SELECT 语句中构建计算列,
- 但我想使用此列来确定后续列(所以……添加计算列在某种程度上是一种解决方法,以避免我不确定我能弄清楚的复杂 CTE - 让我知道 CTE 或函数是否更好走的路)
--- 我希望 CASE 语句成为表本身的计算列
SELECT top 1000
L.[MsgDate]
,C.[AlarmType]
,L.[Type]
,L.[UsrStr1]
,L.[UsrStr4]
,L.[UsrStr5]
,L.[UsrStr12]
,L.[UsrStr15]
,CASE
WHEN EXISTS
(
SELECT *
FROM Breinigsville.dbo.SCADA_SR S
WHERE S.SCADA_SR_TAG = L.UsrStr15 and
((L.[UsrStr4]='analog' and C.[AlarmType] like '%HH%') or (L.[UsrStr4]='status'))
)
THEN 1
ELSE 0
END AS [Safety]
FROM [Breinigsville].[dbo].[LMFW] L
full outer join [Breinigsville].[dbo].[_AcknowledgedAlarms] C
on
L.SeqNo=C.SeqNo2
WHERE (
L.[Type]='Ack' AND
L.UsrStr12 LIKE '%CCXOS%' AND
L.UsrStr12 NOT LIKE '%CCXOS5' AND
L.UsrStr12 NOT LIKE '%CCXOS6' AND
L.UsrStr12 NOT LIKE '%CCXOS9' AND
L.UsrStr12 NOT LIKE '%CCXOS12' AND
L.MsgDate>getdate()-1
)
order by L.SeqNo desc