【问题标题】:Requiring multiple Case When conditions to output one THEN需要多个 Case When 条件输出一个 THEN
【发布时间】:2018-09-16 15:44:52
【问题描述】:

我正在尝试编写一个自定义表达式,指出如果 x、y 和 z 都是空白的,那么从给定日期到今天的 getdate。这就是我现在所拥有的,但我不确定是否需要为 3 个字段中的每一个字段使用不同的 WHEN/THEN 语句,或者是否有办法将它们与 AND 或逗号组合。

Case 
 when UnderwritingSuspendedDate is null then datediff(dd,SubmittedToUnderwritingDate,getdate()) 
 when Milestoneapprovedwithconditions is null then datediff(dd,SubmittedToUnderwritingDate,getdate()) 
 when UnderwritingFinalApprovalDate is null then datediff(dd,SubmittedToUnderwritingDate,getdate()) 
END

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。

标签: sql expression case-when


【解决方案1】:

按照你的逻辑,应该是:

(case when UnderwritingSuspendedDate is null and 
           Milestoneapprovedwithconditions is null and 
           UnderwritingFinalApprovalDate is null 
      then datediff(day, SubmittedToUnderwritingDate, getdate()) 
 end);

请注意,如果三个值中的任何一个不是NULL,则表达式将返回NULL

【讨论】:

  • 太棒了!比我尝试做的要简单得多。感谢您的帮助和快速响应
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 2021-11-04
  • 1970-01-01
  • 2021-08-04
  • 2022-08-17
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
相关资源
最近更新 更多