[英]SQL Case Expression (Conditional)
This is my CASE Expression:这是我的 CASE 表达式:
case when count(distinct fcia.UserKey) > 1 and dd.FullDate between @CalcDate and @DefaultDate
then count(distinct fcia.UserKey)
else case when count(distinct fcia.UserKey) > 1 and dd.FullDate not between @CalcDate and @DefaultDate
then 0
else -1
end
end as StudentCount,
I would like to only display 1 condition, for example:我只想显示 1 个条件,例如:
If the count > 1 and between the two dates then display如果计数 > 1 并且在两个日期之间,则显示
If the count > 1 and NOT BETWEEN dates then ONLY display if not equal to the above condition.如果计数 > 1 且 NOT BETWEEN 日期,则仅在不等于上述条件时才显示。
Lastly, else -1 if both conditions above do not equate.最后,如果上述两个条件不相等,则 else -1。
Your current attempt is close.您当前的尝试已接近尾声。 The correct syntax is:正确的语法是:
CASE WHEN (condition1) THEN ... WHEN (condition2) THEN ... ELSE ... END
So use this CASE
statement:所以使用这个CASE
语句:
CASE WHEN COUNT(DISTINCT fcia.UserKey) > 1 AND
dd.FullDate BETWEEN @CalcDate AND @DefaultDate
THEN COUNT(DISTINCT fcia.UserKey)
WHEN COUNT(DISTINCT fcia.UserKey) > 1 AND
dd.FullDate NOT BETWEEN @CalcDate AND @DefaultDate
THEN 0
ELSE -1
END AS StudentCount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.