SQL第二个Case语句关闭第一个Case语句';s柱
我正在编写一个带有两个Case-When语句的查询,第二个语句使用第一个语句的状态来确定值。但是,我在第2条语句中得到一个无效列错误。有人知道解决这个问题的简单方法吗?谢谢SQL第二个Case语句关闭第一个Case语句';s柱,sql,case-when,Sql,Case When,我正在编写一个带有两个Case-When语句的查询,第二个语句使用第一个语句的状态来确定值。但是,我在第2条语句中得到一个无效列错误。有人知道解决这个问题的简单方法吗?谢谢 select a.ID ,ss.Date ,ss.Name ,ss.Payload ,case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End as [Error Statu
select
a.ID
,ss.Date
,ss.Name
,ss.Payload
,case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End as [Error Status]
,case when
[Error Status] = 'Not Resolved' --Invalid Column issue occurring with [Error Status] here
then 'No Resolution' Else a.Date End as [Date],
,GETDATE() [Insert Date]
from #Errors a
join Table1 ss on a.id = ss.Id and a.Date = ss.Date
order by a.Date desc
我将第一个case表达式嵌套在第二个case表达式中
select
a.ID
,ss.Date
,ss.Name
,ss.Payload
,case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End as [Error Status]
,case when
case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End = 'Not Resolved' --Invalid Column issue occurring with [Error Status] here
then 'No Resolution' Else a.Date End as [Date],
,GETDATE() [Insert Date]
from #Errors a
join Table1 ss on a.id = ss.Id and a.Date = ss.Date
order by a.Date desc
在第二个
案例
表达式中,您只需再次检查statusdescription
的值:
SELECT
a.ID,
ss.Date,
ss.Name,
ss.Payload,
CASE WHEN statusdescription = 'Bad Request'
THEN 'Not Resolved' ELSE 'Resolved' END AS [Error Status],
CASE WHEN statusdescription = 'Bad Request'
THEN 'No Resolution' ELSE a.Date END AS [Date],
GETDATE() AS [Insert Date]
FROM #Errors a
INNER JOIN Table1 ss
ON a.id = ss.Id AND
a.Date = ss.Date
ORDER BY a.Date DESC
请注意,如果此查询未运行,您可能必须将a.Date
转换为第二个CASE
表达式中的文本。CASE表达式,而不是语句…我刚刚做了一个自己的测试(Sql Server 2014),并且我能够从后面的一个CASE列引用前面的CASE列,因此这是可能的(在Sql Server 2014中)。
select
a.ID
,ss.Date
,ss.Name
,ss.Payload
,case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End as [Error Status]
,case when
case when statusdescription = 'Bad Request' then 'Not Resolved' Else 'Resolved' End = 'Not Resolved' --Invalid Column issue occurring with [Error Status] here
then 'No Resolution' Else a.Date End as [Date],
,GETDATE() [Insert Date]
from #Errors a
join Table1 ss on a.id = ss.Id and a.Date = ss.Date
order by a.Date desc