2017-08-22 48 views
1

請不要向我扔石頭,我知道已經有一些關於類似問題的問題(特別是this question),但與最後一個不同,我的問題有點不同。比方說,我有一個子查詢返回相似的結果如下:當一天中的某一天丟失時插入一行空值的SQL

+--------+--------------------------+-----------+----------+ 
| JOB_ID | START_TIME    | STATUS | JOB_NAME | 
+--------+--------------------------+-----------+----------+ 
| 1234 | 17-08-01 16:15:29,251000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
| 1235 | 17-08-03 16:25:20,931000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
| 1236 | 17-08-07 16:15:27,452000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
| 1237 | 17-08-09 07:50:27,661000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
| 1238 | 17-08-10 16:19:19,554000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 

我想什麼它做的是,當我缺少充滿除了START_TIME列誰也只是增加空值天要添加新行到一天。結果爲例,我想:

+--------+--------------------------+-----------+----------+ 
| JOB_ID | START_TIME    | STATUS | JOB_NAME | 
+--------+--------------------------+-----------+----------+ 
| 1234 | 17-08-01 16:15:29,251000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
|  | 17-08-02 00:00:00,000000 |   |   | 
+--------+--------------------------+-----------+----------+ 
| 1235 | 17-08-03 16:25:20,931000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
|  | 17-08-04 00:00:00,000000 |   |   | 
+--------+--------------------------+-----------+----------+ 
|  | 17-08-05 00:00:00,000000 |   |   | 
+--------+--------------------------+-----------+----------+ 
|  | 17-08-06 00:00:00,000000 |   |   | 
+--------+--------------------------+-----------+----------+ 
| 1236 | 17-08-07 16:15:27,452000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
|  | 17-08-08 00:00:00,000000 |   |   | 
+--------+--------------------------+-----------+----------+ 
| 1237 | 17-08-09 07:50:27,661000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 
| 1238 | 17-08-10 16:19:19,554000 | COMPLETED | EXTRACT | 
+--------+--------------------------+-----------+----------+ 

我知道我可能會問了很多在這裏,但如果你認爲你能幫助我將不勝感激!

+1

你確定job_id是NULL,它會是主鍵嗎? –

回答

1

DSET正在設置數據。在CSET中,我們生成缺失的行。

WITH 
    dset 
    AS 
     (SELECT 1234 job_id 
       , TO_DATE ('17-08-01 16:15:29', 'YY-MM-DD HH24:mi:SS') start_time 
       , 'COMPLETED' status 
       , 'EXTRACT' job_name 
      FROM DUAL 
     UNION ALL 
     SELECT 1235 job_id 
       , TO_DATE ('17-08-03 16:25:20', 'YY-MM-DD HH24:mi:SS') start_time 
       , 'COMPLETED' status 
       , 'EXTRACT' 
      FROM DUAL 
     UNION ALL 
     SELECT 1236 job_id 
       , TO_DATE ('17-08-07 16:15:27', 'YY-MM-DD HH24:mi:SS') start_time 
       , 'COMPLETED' status 
       , 'EXTRACT' job_name 
      FROM DUAL 
     UNION ALL 
     SELECT 1237 job_id 
       , TO_DATE ('17-08-09 07:50:27', 'YY-MM-DD HH24:mi:SS') start_time 
       , 'COMPLETED' status 
       , 'EXTRACT' 
      FROM DUAL 
     UNION ALL 
     SELECT 1238 job_id 
       , TO_DATE ('17-08-10 16:19:19', 'YY-MM-DD HH24:mi:SS') start_time 
       , 'COMPLETED' status 
       , 'EXTRACT' 
      FROM DUAL), 
    aset (job_id, start_time, status 
     , job_name, next_start_time) 
    AS 
     (SELECT job_id 
       , start_time 
       , status 
       , job_name 
       , LEAD (start_time) 
        OVER (
         PARTITION BY job_name ORDER BY start_time 
        ) 
        next_start_time 
      FROM dset 
     UNION ALL 
     SELECT NULL 
       , TRUNC (start_time + 1) 
       , status 
       , NULL 
       , next_start_time 
      FROM aset 
      WHERE TRUNC (start_time + 1) < TRUNC(next_start_time)) 
SELECT job_id 
    , start_time 
    , status 
    , job_name 
    FROM Aset ORDER BY START_TIME; 

JOB_ID START_TIME    STATUS JOB_NAME 
1234 2017/08/01 4:15:29 PM COMPLETED EXTRACT 
     2017/08/02    COMPLETED   
1235 2017/08/03 4:25:20 PM COMPLETED EXTRACT 
     2017/08/04    COMPLETED   
     2017/08/05    COMPLETED   
     2017/08/06    COMPLETED   
1236 2017/08/07 4:15:27 PM COMPLETED EXTRACT 
     2017/08/08    COMPLETED   
1237 2017/08/09 7:50:27 AM COMPLETED EXTRACT 
1238 2017/08/10 4:19:19 PM COMPLETED EXTRACT 
+0

絕對看起來很有趣,我會盡力而爲,儘快回覆你! – WWWubzy

+0

我測試了一下,它的工作原理與我想要的完全一樣!我只是改變了我自己的子查詢dset,並改變了選擇一點,它的工作完美無瑕!非常感謝! – WWWubzy

+0

經過進一步測試後,我意識到,當失蹤的條目是本月的第一個(本例中爲2017/08/01)時,它跳過它並從2017年8月2日開始。有關我如何解決此問題的任何線索? – WWWubzy