我有一個聲明,崩潰的日期範圍,我得到正確的摺疊版本,當通過iteself執行SQL,並將它插入到嵌套表過程我得到一行應該已經摺疊到另一行。我得到從不同位置執行相同的語句的2個不同的行數
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個不同的結果,只是執行的方式不同,它是根據它執行其中的操作不同的順序。
此外,額外的記錄本質上是一個負的時間跨度,結束日期在開始日期之前,但這是在查詢中處理。
您可以添加該client_pk和plan_id的原始數據嗎?我很確定這是因爲你濫用了'ROWNUM',但是我想在重寫它之前能夠重現它。 –