2012-08-22 42 views
1

試圖填寫僅在表中的值發生更改時纔會更新的表上報告用途的差距。Oracle數據密度填充間隙

enter image description here

代碼:

WITH 
week_list AS --List of weeks 
(
    SELECT ( (trunc(to_date('06152012','mmddyyyy'), 'IW')+7) - (LEVEL * 7))+6 as week 
     from dual 
     connect by level <= 7 
    order by ((trunc(to_date('06152012','mmddyyyy'), 'IW')+7) - (LEVEL * 7))+6 
) 
SELECT 
    product, 
    week, 
    undist_amt_eod as quantity, 
    LAST_VALUE(undist_amt_eod IGNORE NULLS) OVER (PARTITION BY product ORDER BY week) repeated_quantity 
FROM 
(
    SELECT 
     product, 
     week_list.week, 
     inv_table.undist_amt_eod 
    FROM 
     inv_table PARTITION BY (product) 
     RIGHT OUTER JOIN week_list ON (week_list.week = inv_table.history_date) 
    where 
     inv_table.tab = '*' --Includes all types of this product 
) 
ORDER BY product, week; 

周列表輸出示例:

enter image description here

表內容:注意表格每天可以有多個標籤。 *是當天所有選項卡的總和,所以我只對*值感興趣。

enter image description here

基於我從Oracle例如我的代碼中發現here。不確定爲什麼數據輸出仍然不密集。

+0

是那裏的第6和第20的數據? – tbone

+0

負面沒有。 – Cimplicity

回答

3

的問題是你的WHERE子句:

where inv_table.tab = '*' --Includes all types of this product 

因爲你正在做一個右外連接,inv_table.tab將是空當沒有比賽。將其更改爲類似下面的一個:

where inv_table.tab = '*' or inv_table.history_date is null --Includes all types of this product 

或者,如果inv_table.tab不能爲NULL,則:

where coalesce(inv_table.tab, '*') = '*' 
+1

+1雖然我個人更傾向於將'和inv_table.tab ='*''添加到JOIN條件中。 –