1

テーブルの値が変更されたときにのみ更新されるテーブルで、レポート目的でギャップを埋めようとする。

ここに画像の説明を入力

コード:

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;

週リストの出力例:

ここに画像の説明を入力

テーブルの内容:テーブルには 1 日に複数のタブを含めることができることに注意してください。* はその日のすべてのタブの合計なので、* の値だけに関心があります。

ここに画像の説明を入力

ここにあるオラクルの例に基づいてコードを作成しました。データ出力がまだ密集していない理由が不明です。

4

1 に答える 1

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, '*') = '*'
于 2012-08-23T03:12:00.533 に答える