2015-06-04 74 views
1

我試圖用SQL把我的當前查詢結果:填寫數據和失蹤日期

 
+------------+------------+------------+ 
| STORE_NUM | PRICE  | DATE_CHNG | 
+------------+------------+------------+ 
| 100  | 1.50  | 2014-05-01 | 
| 100  | 1.52  | 2014-05-03 | 
| 100  | 1.48  | 2014-05-05 | 
| 100  | 1.51  | 2014-05-10 | 
+------------+------------+------------+ 

,並讓他們看起來更像是這個,在這裏添加缺少的日期,但值了價格也填寫:

 
+------------+------------+------------+ 
| STORE_NUM | PRICE  | DATE_CHNG | 
+------------+------------+------------+ 
| 100  | 1.50  | 2014-05-01 | 
| 100  | 1.50  | 2014-05-02 | 
| 100  | 1.52  | 2014-05-03 | 
| 100  | 1.52  | 2014-05-04 | 
| 100  | 1.48  | 2014-05-05 | 
| 100  | 1.48  | 2014-05-06 | 
| 100  | 1.48  | 2014-05-07 | 
| 100  | 1.48  | 2014-05-08 | 
| 100  | 1.48  | 2014-05-09 | 
| 100  | 1.51  | 2014-05-10 | 
+------------+------------+------------+ 

任何幫助/建議/資源非常感謝。

謝謝!

+4

創建日期幫助表,加入。 – jarlh

+1

在代碼中執行它而不是您的查詢。 – tvanfosson

+0

如果失敗,請使用google。 –

回答

1

嘗試:

SELECT "STORE_NUM", "PRICE", "DATE_CHNG" + x - 1 As "DATE_CHNG" 
FROM (
    SELECT *, 
     Lead("DATE_CHNG") OVER (Order By "DATE_CHNG") - "DATE_CHNG" As number_of_days 
    FROM table1 
) m, 
LATERAL (
    SELECT generate_series(1, 
         CASE coalesce(m.number_of_days, 1) 
         WHEN 0 THEN 1 
         ELSE coalesce(m.number_of_days, 1) END) x 
) y 

演示==>http://sqlfiddle.com/#!15/3c5a47/2


此查詢適用於9.3版本只,更早的版本不支持橫向加入