我有这张桌子:
ID Date
-----------------
1 1/1/2019
1 1/15/2019
预期输出:
ID DATE LEAD_DATE
-------------------------
1 1/1/2019 1/14/2019
1 1/15/2019 SYSDATE
SQL:
SELECT
*,
CASE
WHEN LEAD (a.date) OVER (PARTITION BY a.ID ORDER BY a.date) = TRUNC(a.date) THEN NULL
ELSE LEAD (a.date) OVER (PARTITION BY a.id ORDER BY a.date) - NUMTODSINTERVAL(1,'second')
END AS LEAD_DT
FROM a
结果:
ID DATE LEAD_DATE
-------------------------
1 1/1/2019 1/14/2019
1 1/15/2019
我可以在案例表达式中使用null
时添加系统日期吗?
答案 0 :(得分:2)
使用NVL
:
SELECT
a.*,
NVL(CASE
WHEN LEAD (a.date) OVER (PARTITION BY H.ID ORDER BY a.date) = TRUNC(a.date) THEN NULL
ELSE LEAD (a.date) OVER (PARTITION BY a.id ORDER BY a.date) - NUMTODSINTERVAL(1,'second')
END, SYSDATE) AS LEAD_DT
FROM a
或者,更好的是:
SELECT
a.*,
CASE LEAD (a.date) OVER (PARTITION BY a.ID ORDER BY a.date)
WHEN TRUNC(a.date) THEN SYSDATE
WHEN NULL THEN SYSDATE
ELSE LEAD (a.date) OVER (PARTITION BY a.id ORDER BY a.date) - NUMTODSINTERVAL(1,'second')
END AS LEAD_DT
FROM a
答案 1 :(得分:0)
使用COALESCE
:。
SELECT a.*,
CASE COALESCE(LEAD("Date") OVER (PARTITION BY ID ORDER BY "Date") - "Date", 0)
WHEN 0 THEN SYSDATE
ELSE LEAD("Date") OVER (PARTITION BY ID ORDER BY "Date") - INTERVAL '1' SECOND
END AS LEAD_DT
FROM a