[英]Case when then in where clause
How to use case when then
in where
clause if one of the conditions is fulfilled?What I need is if there is only one condition to be executed after the first condition is met If a condition meets the following conditions, it will not be executed.The case statement will stop evaluating further as soon as the first when condition is met如果满足其中一个条件
case when then
在where
子句中如何使用case when then
? 我需要的是如果满足第一个条件后只有一个条件要执行,如果条件满足以下条件,则不会执行。一旦满足条件,case 语句将停止进一步评估
SELECT TOP 100 ProjType, projCost, InvestAmount, ProjBuildSize
FROM dbo.Project
WHERE
(
CASE
WHEN projCost > 0 then projCost end >= 10000000
WHEN InvestAmount IS NOT NULL then InvestAmount end >= 12000000
WHEN ProjBuildSize > 0 then ProjBuildSize ELSE >= 5000
END
)
You don't need CASE WHEN
here, a simple x OR
y OR
z will suffice.你不需要
CASE WHEN
,一个简单的 x OR
y OR
z 就足够了。
Also, if InvestAmount
is a nullable column, then to check for null you must do InvestAmount IS NOT NULL
instead of InvestAmount <> NULL
此外,如果
InvestAmount
是一个可为空的列,那么要检查是否为空,您必须执行InvestAmount IS NOT NULL
而不是InvestAmount <> NULL
SELECT TOP 100 ProjType,projCost,InvestAmount,ProjBuildSize
FROM dbo.Project
WHERE
(projCost > 0 AND projCost >= 10000000)
OR (InvestAmount IS NOT NULL AND InvestAmount >= 12000000)
OR (WHEN ProjBuildSize > 0 AND ProjBuildSize >= 5000)
You can use the case
statement like this in your where
clause:您可以在
where
子句中使用这样的case
语句:
SELECT TOP 100 ProjType, projCost, InvestAmount, ProjBuildSize
FROM dbo.Project
WHERE
(
CASE
WHEN projCost > 0 AND projCost >= 10000000 then 1
WHEN InvestAmount IS NOT NULL AND InvestAmount >= 12000000 then 1
WHEN ProjBuildSize > 0 AND ProjBuildSize >= 5000 then 1
ELSE 0
END
) = 1
If any of your when
conditions are satisfied, the case
will return a 1
, and the where
clause condition will be satisfied, otherwise the case
will return 0
.如果你的任何一个
when
条件满足, case
将返回1
,并且where
子句条件将被满足,否则case
将返回0
。
You can achieve it in this simple way您可以通过这种简单的方式实现它
SELECT TOP 100 ProjType,projCost,InvestAmount,ProjBuildSize FROM dbo.Project
WHERE (projCost <= 0 OR projCost >= 10000000)
AND (projCost <= 0 And ((InvestAmount is null) OR InvestAmount >= 12000000))
AND ((InvestAmount is null And projCost <= 0) And ((ProjBuildSize <= 0 ) OR ProjBuildSize >= 5000))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.