有人能解释一下下面的例子吗?
查询
SELECT (CASE WHEN (GETDATE() - GETDATE()) < 31 THEN 1 ELSE 0 END) [Result],
(CASE WHEN (GETDATE() - '2020-08-30') < 31 THEN 1 ELSE 0 END) [Result1],
(CASE WHEN (GETDATE() - '2020-07-30') < 31 THEN 1 ELSE 0 END) [Result2],
(CASE WHEN (GETDATE() - '2020-07-30') < 31 THEN 1 ELSE 0 END) [Result3]
输出
Result Result1 Result2 Result3
1 1 0 0
我想知道Result
和Result1
如何拥有1
和else 0
实际查询
SELECT ((CASE WHEN (GETDate()-[InvBillDate])<31 then 1
else
(case when (GETDate()-[InvBillDate])<61 And (GETDate()-[InvBillDate])>30 then 2
else
(case when (GETDate()-[InvBillDate])<91 And (GETDate()-[InvBillDate])>60 then 3
else 4 end)
end) end)) AS [ColNo]
FROM [dbo].[Invoice] (NOLOCK)
发布于 2020-09-09 17:58:02
如果你不介意的话,我建议用这种方式来缩短查询。
SELECT (
(CASE WHEN (GETDate()-[InvBillDate])<31 then 1
else
(case when (GETDate()-[InvBillDate])<61 then 2
else
(case when (GETDate()-[InvBillDate])<91 then 3
else 4
end) end) end)
) AS [ColNo]
FROM [dbo].[Invoice] (NOLOCK)
发布于 2020-09-09 17:51:00
在上下文有限的情况下,我将尝试解释查询。
它试图计算今天和账单日期之间的日期差异,然后根据这一差异将日期差异分为4组:
https://stackoverflow.com/questions/63815425
复制相似问题