sql case when...then...else...end 选择判断

达到的需求为:

吓数收回日期为空:
当接单日期不等于空和当天减接单日期大于3天时,为1,否则为0;
当接单日期为空、最大发织交期不等于空和当天减去最大发织交期大于3天时,为1,否则为0;
当接单日期和发织交期都为空,为1,否则为0;
吓数收回日期不为空,为0。

case when (isnull(v._RDate,'')='')
     then  case when (isnull(p.xCDate,'')<>'')
                then case when (datediff(day,p.xCDate,getdate())>3) then 1 else 0 end
        else
           case when ((isnull(p.xCDate,'')='') and (isnull(v.xJHDate,'')<>''))
                then case  when (datediff(day,v.xJHDate,getdate())>3) then 1 else 0 end
        else 
           case when ((isnull(p.xCDate,'')='') and (isnull(v.xJHDate,'')='')) then 1 else 0 end
                end
           end
     else 0 end as N'吓数是否延期',    

主要是找到逻辑就好了

原文地址:https://www.cnblogs.com/Michael-D/p/4095445.html