Oracle数据密集填补空白

时间:2012-08-22 21:49:14

标签: sql database 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

我根据找到的here的oracle示例创建了代码。不确定为什么数据输出仍然不密集。

1 个答案:

答案 0 :(得分:3)

问题是你的WHERE子句:

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

因为您正在进行右外连接,所以当没有匹配时,inv_table.tab将为NULL。将其更改为以下之一:

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, '*') = '*'
相关问题