案例评估第一条件

时间:2016-02-01 14:20:42

标签: sql-server-2008

对案例陈述有一点疑问。我试图找到两个日期之间的DateDiff,我发现一条记录我得到了Datediff(dd,StartDate,EndDate)= 0.而不是显示Green显示Red.Its评估第一个case语句。如何获得红色我的声明如下?请帮忙

 Case     
 When A.key =1 then
 Case     
 when  Datediff(dd,StartDate,EndDate) <90 then 'Red'
  when  Datediff(dd,StartDate,EndDate) <0 and  Datediff(dd,StartDate,EndDate) >=-89   then 'yellow'
 when  Datediff(dd,StartDate,EndDate) >=0 then 'Green'
 when Datediff(dd,StartDate,EndDate) is null then 'Grey' 
 else 'Blue'
 end
  else
 ...........................

1 个答案:

答案 0 :(得分:0)

问题在于它是否满足您为第一个WHEN设置的条件,因为0小于90.

您需要安排条件以使它们互相排斥,例如:

CASE  
    WHEN DATEDIFF(dd,StartDate,EndDate) IS NULL THEN 'GREY' 
    WHEN DATEDIFF(dd,StartDate,EndDate) >= -89 AND DATEDIFF(dd,StartDate,EndDate) < 0 THEN 'YELLOW'
    WHEN DATEDIFF(dd,StartDate,EndDate) >= 0 AND DATEDIFF(dd,StartDate,EndDate) < 90 THEN 'RED'
    WHEN DATEDIFF(dd,StartDate,EndDate) >= 90 THEN 'GREEN'
END

请注意,您设置的方式,永远不会满足您的ELSE(即Blue),因为其他WHEN条款涵盖了所有可能的值。