在Case语句中使用计算 - Oracle SQL

时间:2014-07-02 01:03:50

标签: sql oracle case

你好sql向导和巫师,

我正在尝试根据特定参数(案例类型)计算SLA日期。对于每种案例类型,SLA日期的计算方式略有不同。我的问题是:我是否使用CASE STATEMENT来实现这一目标?我会放下我到目前为止所做的事情,这是行不通的:

fifthlevel --with statement declaring sla days
AS (SELECT

     CASE sla_days
     WHEN fourthlevel.case_type = 'Complaint'
     THEN sla_days =

      ( SELECT COUNT (*)
        FROM (SELECT business_date
              FROM ( SELECT TO_DATE ('01-01-2011', 'dd-mm-yyyy') + LEVEL - 1
                               business_date
                     FROM DUAL
                     CONNECT BY LEVEL <=
                                 TO_DATE ('31-12-2099', 'dd-mm-yyyy')
                               - TO_DATE ('01-01-2011', 'dd-mm-yyyy')
                               + 1) date_tab1
                     WHERE TO_CHAR (business_date, 'DY') NOT IN ('SAT', 'SUN')
                     AND business_date NOT IN (SELECT holiday_dt
                                               FROM cisadm.ci_cal_hol
                                               WHERE calendar_cd = 'WAW01'
                                              )
                   ) work_days1
             WHERE work_days1.business_date > fourthlevel.correspondence_date
             AND work_days1.business_date <= fourthlevel.close_date
             )
     WHEN fourthlevel.case_type = 'Enquiry'
     THEN sla_days = (SELECT COUNT (*)
                      FROM (SELECT business_date
                            FROM (SELECT TO_DATE ('01-01-2011',
                                                   'dd-mm-yyyy')
                                            + LEVEL
                                            - 1
                                               business_date
                                   FROM DUAL
                                   CONNECT BY LEVEL <=TO_DATE('31-12-2099',
                                                                'dd-mm-yyyy')
                                                      TO_DATE('01-01-2011',
                                                              'dd-mm-yyyy')+ 1
                                  ) date_tab1
                            WHERE TO_CHAR (business_date, 'DY') 
                                  NOT IN ('SAT', 'SUN')
                             AND business_date 
                                  NOT IN (SELECT holiday_dt
                                          FROM cisadm.ci_cal_hol
                                          WHERE calendar_cd = 'WAW01'
                                         )
                            ) work_days1
                      WHERE work_days1.business_date > fourthlevel.agreed_Date
                        AND work_days1.business_date <=
                               fourthlevel.close_date
                     )
     END
    FROM fourthlevel,

    fourthlevel.* --also wanting to select * from preceding WITH statement
    FROM fourthlevel)

好的,所以看起来有点乱,但基本上我试图根据'投诉'和'询问'的case_types进行两种不同的计算。

有人能指出我正确的方向吗?

如果我能提供任何进一步的信息,请告诉我。

传说!

2 个答案:

答案 0 :(得分:0)

似乎两个case_type之间的唯一区别是work_days1.business_date的条件。因此,您可以压缩查询,如下所示:

fifthlevel --with statement declaring sla days
AS (
SELECT
  COUNT (*) sla_days
FROM
  (
    SELECT
      business_date
    FROM
      (
        SELECT
          TO_DATE ('01-01-2011', 'dd-mm-yyyy') + LEVEL- 1 business_date
        FROM
          DUAL
          CONNECT BY LEVEL <= TO_DATE ('31-12-2099','dd-mm-yyyy') - TO_DATE ('01-01-2011','dd-mm-yyyy') + 1
      )
      date_tab1
    WHERE TO_CHAR (business_date, 'DY') NOT IN ('SAT', 'SUN')
    AND business_date NOT IN (SELECT holiday_dt FROM cisadm.ci_cal_hol WHERE calendar_cd = 'WAW01')
  )
  work_days1 INNER JOIN fourthlevel
ON
  work_days1.business_date > (
    CASE
      WHEN fourthlevel.case_type = 'Complaint' THEN fourthlevel.correspondence_date
      WHEN fourthlevel.case_type = 'Enquiry' THEN fourthlevel.agreed_Date
    END)
AND work_days1.business_date <= fourthlevel.close_date;

答案 1 :(得分:0)

将work_days1子查询作为单独的cte:

提取
, work_days1 as (
SELECT business_date
       FROM (    SELECT TO_DATE ('01-01-2011', 'dd-mm-yyyy') + LEVEL - 1
                           business_date
                   FROM DUAL
             CONNECT BY LEVEL <=
                             TO_DATE ('31-12-2099', 'dd-mm-yyyy')
                           - TO_DATE ('01-01-2011', 'dd-mm-yyyy')
                           + 1) date_tab1
      WHERE     TO_CHAR (business_date, 'DY') NOT IN ('SAT', 'SUN')
            AND business_date NOT IN (SELECT holiday_dt
                                        FROM cisadm.ci_cal_hol
                                       WHERE calendar_cd = 'WAW01')),
fifthlevel --with statement declaring sla days
 AS (SELECT
 CASE 
 WHEN fourthlevel.case_type = 'Complaint'
 THEN (SELECT COUNT(*) FROM work_days1 WHERE
 work_days1.business_date > fourthlevel.correspondence_date
    AND work_days1.business_date <= fourthlevel.close_date)
 WHEN fourthlevel.case_type = 'Enquiry'
 THEN sla_days = (SELECT COUNT (*) work_days1 WHERE
 work_days1.business_date > fourthlevel.agreed_Date
                    AND work_days1.business_date <=
                           fourthlevel.close_date) END as sla_days, * 
      FROM fourthlevel)