嵌套Case / If where子句中的Else语句

时间:2014-07-11 03:04:47

标签: sql oracle if-statement case

我正在尝试提供我在案例陈述中进行计算的替代方法。目前它正在关注' close_date'为了满足这个等式。我希望它能够看到' close_date'首先,但如果它为null,则使用SYSDATE。

我的代码如下:

    SELECT CASE
               WHEN fourthlevel.case_type IN
                       ('Complaint')
               THEN
                  (SELECT COUNT (*)
                     FROM work_days1
                    WHERE     work_days1.business_date >
                                 fourthlevel.correspond_date
                          AND work_days1.business_date <=
                                 fourthlevel.close_date)

               WHEN fourthlevel.case_type IN ('Enquiry')
               THEN
                  (SELECT COUNT (*)
                     FROM work_days1
                    WHERE     work_days1.business_date >
                                 fourthlevel.create_date
                          AND work_days1.business_date <=
                                 fourthlevel.close_date)
            END
               AS sla_days
      FROM fourthlevel

那么我在where子句或其他case语句中使用嵌套的if-else语句吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Coalesce

http://msdn.microsoft.com/en-us/library/ms190349.aspx

Coalesce检查第一个参数;如果为null - 它使用第二个参数,否则它使用第一个参数。

COALESCE(fourthlevel.close_date, SYSDATE)

如果close_date为空,则检查fourthlevel.close_date然后使用SYSDATE

相关问题