sql - Oracle 数据致密化填补空白

标签 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

我的代码基于找到的 oracle 示例 here .不确定为什么数据输出仍然不密集。

最佳答案

问题是你的 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, '*') = '*'

关于sql - Oracle 数据致密化填补空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081911/

相关文章:

c# - 跳过/采用两个合并列表的策略

python - 在 Django 中有一种方法可以根据相关对象的条件聚合关系

mysql - 如何计算数据库中的属性

oracle - Oracle中的父子关系

sql - 选择查询的 n-1 行

mysql - 多对多关系中的外键

sql - Oracle 表引用另一个模式中的表

mysql - SQL 数据库列中的标题大小写值?

c# - 使用 shell 命令从 C# 程序中下载 MySQL 表是否合适?

sql - 没有分组依据的 Oracle 结果