[英]SQL Case Statement: Inside Where Clause
I have read some other Q&A about case statements inside the 'WHERE' clause, but I cannot truly understand how to use it. 我已经阅读了有关“ WHERE”子句中的case语句的其他问答,但我无法真正理解如何使用它。 I will post below a snippet of the code.
我将在代码片段下方发布。 I believe I am ignorant of a fundamental principle concerning how to use a case statement, and this is why the code will not compile/run.
我相信我对有关如何使用case语句的基本原理一无所知,这就是为什么代码不会编译/运行的原因。 I appreciate any help.
感谢您的帮助。
where i.status IN ('CR','L','O')
and i.FGCs > 0
and i.LastShpd > CAST(CONVERT(CHAR(11),DATEADD(DAY,-180,GETDATE()),113) AS datetime)
and (Case
When n.OnOrder IN ('0', '')
Then i.OnOrder = 0 or i.LastShpd < CAST(CONVERT(CHAR(11),DATEADD(DAY,-21,GETDATE()),113) AS datetime)))
End)
Order by i.LastShpd desc 由i.LastShpd desc订购
To explain what I have above, I already got the appropriate 'SELECT' and 'FROM' statement. 为了解释上面的内容,我已经获得了适当的“ SELECT”和“ FROM”语句。 Now I am filtering the results based on those the shown variables (ecx LastShpd).
现在,我根据显示的变量(ecx LastShpd)过滤结果。 What I wanted the case statement to do was: When n.OnOrder = 0, I want to keep only the rows where i.OnOrder = 0 or if i.LastShpd has been greater than 21 days.
我想让case语句执行以下操作:当n.OnOrder = 0时,我只想保留i.OnOrder = 0或i.LastShpd大于21天的行。
I don't think you need a Case for this: 我认为您不需要为此提供案例:
where i.status IN ('CR','L','O')
and i.FGCs > 0
and i.LastShpd > CAST(CONVERT(CHAR(11),DATEADD(DAY,-180,GETDATE()),113) AS datetime)
and (
(n.OnOrder IN ('0', '') and i.OnOrder = 0)
or i.LastShpd < CAST(CONVERT(CHAR(11),DATEADD(DAY,-21,GETDATE()),113) AS datetime)
)
Re-reading your question maybe is this other way: 重新阅读您的问题可能是这样的:
where i.status IN ('CR','L','O')
and i.FGCs > 0
and i.LastShpd > CAST(CONVERT(CHAR(11),DATEADD(DAY,-180,GETDATE()),113) AS datetime)
and (
n.OnOrder Not IN ('0', '')
or i.OnOrder = 0
or i.LastShpd < CAST(CONVERT(CHAR(11),DATEADD(DAY,-21,GETDATE()),113) AS datetime)
)
When using a CASE
within a WHERE
clause, you still need to have both sides of the operation defined (ie [CASE CONDITION] = [SOMETHING]). 在
WHERE
子句中使用CASE
,仍需要定义操作的两侧(即[CASE CONDITION] = [SOMETHING])。 This can get tricky depending on what you want to do, but the easiest thing is to have your case statement execute as a true/false type of condition so that you end up with [CASE] = 1 or [CASE] = 0. 根据您要执行的操作,这可能会很棘手,但是最简单的方法是让case语句作为条件的真/假类型执行,以便最终得到[CASE] = 1或[CASE] = 0。
In your case (accidental pun!): 就您而言(偶然的双关语!):
where i.status IN ('CR','L','O')
and i.FGCs > 0
and i.LastShpd > CAST(CONVERT(CHAR(11),DATEADD(DAY,-180,GETDATE()),113) AS datetime)
and (Case
When n.OnOrder IN ('0', '') AND (i.OnOrder = 0 or i.LastShpd < CAST(CONVERT(CHAR(11),DATEADD(DAY,-21,GETDATE()),113) AS datetime))
THEN 1
ELSE 0
End) = 1
Of course as another answer has pointed out, a case is not really necessary in this particular instance. 当然,正如另一个答案所指出的那样,在这种特定情况下,案例确实不是必需的。 However, if you had more complex conditions this could be helpful.
但是,如果您的条件比较复杂,这可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.