要求如下:在同一个表中我需要如下预期输出。 表名:TAB 电流输出:
PRIM_KEY| FROM_DT | TO_DT
11111 01-JAN-00 01-JAN-25
11112 01-MAR-16 01-JAN-25
期待输出:
PRIM_KEY| FROM_DT | TO_DT
11111 01-JAN-00 01-MAR-16
11112 01-MAR-16 01-JAN-25
答案 0 :(得分:0)
你可以使用窗口函数并只关注行跟随,最后一行将为null,你可以跳过它。
MERGE
INTO table1 AS A
USING
(
SELECT PRIM_KEY, FROM_DT, MIN(FROM_DT) OVER ( PARTITION BY 1 ORDER BY PRIM_KEY ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) AS NEXT_START_DT
FROM table1) AS B
ON A.PRIM_KEY = B.PRIM_KEY AND B. PRIM_KEY IS NOT NULL
WHEN MATCHED THEN
UPDATE SET TO_DT = NEXT_START_DT;