时间:2021-07-01 10:21:17 帮助过:87人阅读
实现代码,以上代码是造数脚本:
WITH TMP
AS ( SELECT PB_DATE,
SAVE_RATE,
LEN_RATE,
LEAD (PB_DATE, 1, DATE ‘9999-12-31‘) OVER (ORDER BY PB_DATE)
NEXT_DATE,
LAG (PB_DATE, 1, PB_DATE) OVER (ORDER BY PB_DATE) PRE_DATE,
LEAD (SAVE_RATE) OVER (ORDER BY PB_DATE) NEXT_SAVE,
LAG (SAVE_RATE) OVER (ORDER BY PB_DATE) PRE_SAVE,
LEAD (LEN_RATE) OVER (ORDER BY PB_DATE) NEXT_LEN,
LAG (LEN_RATE) OVER (ORDER BY PB_DATE) PRE_LEN
FROM TB
ORDER BY PB_DATE, SAVE_RATE, LEN_RATE),
TMP2
AS ( SELECT MAX (T.PB_DATE) AS PB_DATE, T.SAVE_RATE, T.LEN_RATE
FROM TMP T
WHERE (CASE
WHEN (SAVE_RATE = NEXT_SAVE AND LEN_RATE = NEXT_LEN)
OR (SAVE_RATE = PRE_SAVE AND LEN_RATE = PRE_LEN)
THEN
1
ELSE
0
END) = 1
GROUP BY T.SAVE_RATE, T.LEN_RATE
ORDER BY 1),
TMP3
AS (SELECT PB_DATE,SAVE_RATE,LEN_RATE
FROM TB T
WHERE NOT EXISTS
(SELECT NULL
FROM TMP2 R
WHERE R.SAVE_RATE = T.SAVE_RATE
AND R.LEN_RATE = T.LEN_RATE
AND R.PB_DATE = T.PB_DATE))
SELECT PB_DATE AS START_DATE,
LEAD (PB_DATE, 1, DATE ‘9999-12-31‘) OVER (ORDER BY PB_DATE) - 1
AS END_DATE,
SAVE_RATE,
LEN_RATE
FROM TMP3;
Oracle根据连续性日期的重复数据取最大或最小值日期
标签:ast with for group dual 复数 rac form table