我从不同位置执行的同一语句中得到2个不同的行数

时间:2016-07-29 18:40:05

标签: oracle plsql lag

我有一个声明,它会折叠日期范围,并且我在使用iteself执行SQL时获得正确的折叠版本,并且在将其插入到过程中的嵌套表中时,我得到的行应该已经折叠到另一行

           SELECT  client_pk,
                   plan_id,
                   grp,
                   MIN(start_dt) start_dt, 
                   MAX(end_dt) end_dt
              FROM (
                SELECT client_pk
                       plan_id,
                       start_dt,
                       end_dt,
                       MAX(grp) OVER (PARTITION BY plan_id ORDER BY start_dt ASC) grp 
                    FROM (
                    SELECT mp.client_pk,
                           mp.plan_id,
                           CASE 
                                WHEN (LAG(mp.end_dt) OVER (PARTITION BY mp.plan_id ORDER BY mp.start_dt ASC)) BETWEEN mp.start_dt-1 AND 
                                                                                                                        NVL(mp.end_dt,to_date('12/31/9999','MM/DD/YYYY'))
                                    THEN NULL
                            ELSE ROWNUM
                           END grp,  
                           mp.start_dt,
                           NVL(mp.end_dt,to_date('12/31/9999','MM/DD/YYYY')) end_dt
                      FROM client_plan mp
                          )
                      )
                 GROUP BY grp, plan_id, client_pk

所以我从最里面的查询中得到一个初始结果集,给出一个ROWNUM:

client_pk   PLAN_ID GRP start_dt    end_dt
8752        25171   3   1/1/2016    3/31/2016
8752        25171   1   2/1/2016    1/31/2016

最后,当作为独立查询执行时,它会被正确折叠

client_pk   PLAN_ID GRP start_dt    end_dt
8752        25171   3   1/1/2016    3/31/2016

但是当通过将这些记录转储到嵌套表中然后最终插入到数据库中的过程运行时,仍会返回这两行。

    SELECT plan_spans_obj(client_pk, plan_id, start_dt, end_dt)
      BULK COLLECT INTO plan_spans_ins_tbl
      FROM ( 
       SELECT  client_pk,
               plan_id,
               start_dt, 
               end_dt
         FROM ( SELECT client_pk,
                       plan_id,
                       grp,
                       MIN(start_dt) start_dt, 
                       MAX(end_dt) end_dt
                  FROM (
                    SELECT client_pk
                           plan_id,
                           start_dt,
                           end_dt,
                           MAX(grp) OVER (PARTITION BY plan_id ORDER BY start_dt ASC) grp 
                        FROM (
                        SELECT mp.client_pk,
                               mp.plan_id,
                               CASE 
                                    WHEN (LAG(mp.end_dt) OVER (PARTITION BY mp.plan_id ORDER BY mp.start_dt ASC)) BETWEEN mp.start_dt-1 AND 
                                                                                                                            NVL(mp.end_dt,to_date('12/31/9999','MM/DD/YYYY'))
                                        THEN NULL
                                ELSE ROWNUM
                               END grp,  
                               mp.start_dt,
                               NVL(mp.end_dt,to_date('12/31/9999','MM/DD/YYYY')) end_dt
                          FROM client_plan mp
                              )
                          )
                     GROUP BY grp, plan_id, client_pk
                 )
              );

那么我如何从同一个查询得到2个不同的结果,只是执行不同,是否根据执行的位置而不同的操作顺序。

此外,额外记录本质上是一个负时间跨度,结束日期在开始日期之前,但这是在查询中处理的。

1 个答案:

答案 0 :(得分:0)

答案是我没有初始化该集合。不敢相信它是那样的,但显然是这样。似乎现在正在运作。