2016-10-03 44 views
0

我想按升序排列日期序列中的所有缺失日期。我怎樣才能做到這一點,而不使用任何函數或udfs一個簡單的SQL。獲取日期序列中的所有缺失日期

Input :- 

2016-09-01 
2016-09-02 
2016-09-05 
2016-09-10 

輸出: -

2016-09-03 
2016-09-04 
2016-09-06 
2016-09-07 
2016-09-08 
2016-09-09 

我已經試過?

select start, stop 
    from 
     (
      select m.x + 1 as start, 
       (select min(x) - 1 from X as x where x.x > m.x) as stop 
      from X as m 
      left outer join X as r 
       on m.x = r.x - 1 
      where r.x is null 
     ) as x 
    where stop is not null; 
+0

如果您標記了正在使用的數據庫管理系統,也表明你的嘗試,這將有助於。 –

+0

我想用整數解決它,然後跳轉到日期。 – Teja

+0

你的數據庫支持遞歸公用表表達式嗎? –

回答

-1
  1. 創建一個新表,並插入365行編號1-365 (交替使用已經有超過365行的表,並使用ROWNUM或類似的結構來獲得獨特的整數)

  2. 轉換日期爲整數(在Oracle使用像TO_CHAR(mydate, 'ddd')

  3. 在查詢中聯合起來這兩個列表中找到適當的一組(重疊,缺失等)

  4. 轉換回日期

+0

我正在使用亞馬遜紅移 – Teja

+0

@Teja我不明白Amazon Redshift支持cte。 –

+0

Amazon Redshift不支持遞歸類。檢查這個鏈接。 http://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-features.html – Teja

相關問題