无法理解过程的Oracle逻辑

时间:2018-01-30 05:45:48

标签: oracle

PROCEDURE DELETE_X1
IS
  v_emp_unum   VARCHAR2 (25);
BEGIN
  /*FOR rec
     IN (SELECT l2.*,
       row_number ()
   OVER (PARTITION BY case_uid,nvl(min_eff_date, eff_begin_date)
         ORDER BY case_uid,nvl(min_eff_date, eff_begin_date))
      rw,
                ROWID rid
           FROM (SELECT l1.*,
                        MIN (
                           CASE
                              WHEN eff_end_date = next_begin - 1
                              THEN
                                 eff_begin_date
                           END)
                        OVER (PARTITION BY case_uid)
                           min_eff_date,

                        MAX (
                           CASE
                              WHEN     previous_end <>
                                          TO_DATE ('12/31/9999',
                                                   'mm/dd/yyyy')
                                   AND previous_end + 1 = eff_begin_date
                              THEN
                                 eff_end_date

                           END)
                        OVER (PARTITION BY case_uid)

                           max_end_date
                   FROM (SELECT GT.*,
                                LEAD (
                                   EFF_begin_DATE)
                                OVER (PARTITION BY CASE_UID
                                      ORDER BY EFF_BEGIN_DATE)
                                   next_begin,
                                LAG (
                                   EFF_end_DATE)
                                OVER (PARTITION BY CASE_UID
                                      ORDER BY EFF_BEGIN_DATE)
                                   previous_end
                           FROM TABLE_OUTPUT GT
                          WHERE SRC = 'ERICSSON' AND STATUS_CODE = 'X1') l1)
                l2)*/
  FOR rec
     IN (SELECT l2.*,
       ROW_NUMBER ()
                    OVER (PARTITION BY case_uid, min_eff_date, max_end_date
                          ORDER BY case_uid, min_eff_date, max_end_date)
      rw,
                ROWID rid
           FROM (SELECT l1.*,
                        (MAX (
                               start_at)
                            OVER (
                               PARTITION BY case_uid
                               ORDER BY EFF_BEGIN_DATE
                               ROWS UNBOUNDED PRECEDING))
                           min_eff_date,
                        (MIN (
                               break_at)
                            OVER (
                               PARTITION BY case_uid
                               ORDER BY EFF_BEGIN_DATE
                               ROWS BETWEEN CURRENT ROW
                                    AND     UNBOUNDED FOLLOWING))
                           max_end_date
                   FROM (SELECT GT.*,
                                (CASE
                                         WHEN LAG (
                                                 EFF_end_DATE)
                                              OVER (
                                                 PARTITION BY CASE_UID
                                                 ORDER BY EFF_BEGIN_DATE) =
                                                   EFF_BEGIN_DATE
                                                 - 1
                                         THEN
                                            NULL
                                         ELSE
                                            EFF_BEGIN_DATE
                                      END)
                                   start_at,
                                (CASE
                                         WHEN LEAD (
                                                 EFF_BEGIN_DATE)
                                              OVER (
                                                 PARTITION BY case_uid
                                                 ORDER BY EFF_BEGIN_DATE) =
                                                 CASE
                                                    WHEN EFF_end_DATE <>
                                                            TO_DATE (
                                                               '12/31/9999',
                                                               'mm/dd/yyyy')
                                                    THEN
                                                         EFF_end_DATE
                                                       + 1
                                                    ELSE
                                                       EFF_end_DATE
                                                 END
                                         THEN
                                            NULL
                                         ELSE
                                            EFF_end_DATE
                                      END)
                                   break_at
                           FROM TABLE_OUTPUT GT
                          WHERE SRC = 'ERICSSON' AND STATUS_CODE = 'X1') l1)
                l2)

代码的一部分被注释掉了。

注释掉代码输出

OFF TIME 1/1/2017    1/7/2017  X1 
OFF TIME 1/8/2017    2/1/2017  X1 

新代码输出

OFF TIME    1/1/2017    2/1/2017  X1 
NORMAL      2/2/2017    2/2/2017 AB 
OFF TIME    2/20/2017 

LAG功能用于访问前一行的数据。 LEAD函数用于从结果集中向下的行返回数据。 集合函数MIN,MAX

我对代码流感到困惑。  我无法理解整个代码请解释代码的逻辑

0 个答案:

没有答案