Ведущее и регистр выражения - PullRequest
0 голосов
/ 06 февраля 2019

У меня есть эта таблица:

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 в регистре?

Ответы [ 2 ]

0 голосов
/ 06 февраля 2019

Использование 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
0 голосов
/ 06 февраля 2019

Использование 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
...