の開閉バランス@boneistsアプローチのバリエーションを持っている任意の行を破棄:
しかし、あまりにも同じ結果を得る
with t as (
select 1 id, 'A' name, '2007' year, '04' month, 5 sales from dual union all
select 2 id, 'A' name, '2007' year, '05' month, 2 sales from dual union all
select 3 id, 'B' name, '2008' year, '12' month, 3 sales from dual union all
select 4 id, 'B' name, '2009' year, '12' month, 56 sales from dual union all
select 5 id, 'C' name, '2009' year, '08' month, 89 sales from dual union all
select 13 id,'B' name, '2016' year, '01' month, 10 sales from dual union all
select 14 id,'A' name, '2016' year, '02' month, 8 sales from dual union all
select 15 id,'D' name, '2016' year, '03' month, 12 sales from dual union all
select 16 id,'E' name, '2016' year, '04' month, 34 sales from dual
),
y (year, rnk) as (
select year, dense_rank() over (order by year)
from (select distinct year from t)
),
r (name, year, month, sales, rnk) as (
select t.name, t.year, t.month, t.sales, y.rnk
from t
join y on y.year = t.year
union all
select r.name, y.year, r.month, 0, y.rnk
from y
join r on r.rnk = y.rnk - 1
where not exists (
select 1 from t where t.year = y.year and t.month = r.month and t.name = r.name
)
)
select name, year, month, sales,
nvl(sum(sales) over (partition by name order by year, month
rows between unbounded preceding and 1 preceding), 0) as opening_bal,
nvl(sum(sales) over (partition by name order by year, month
rows between unbounded preceding and current row), 0) as closing_bal
from r
order by year, month, name;
あなたの元のデータから、すべての個別の年を生成
NAME YEAR MONTH SALES OPENING_BAL CLOSING_BAL
---- ---- ----- ---------- ----------- -----------
A 2007 04 5 0 5
A 2007 05 2 5 7
A 2008 04 0 7 7
A 2008 05 0 7 7
B 2008 12 3 0 3
A 2009 04 0 7 7
A 2009 05 0 7 7
C 2009 08 89 0 89
B 2009 12 56 3 59
B 2016 01 10 59 69
A 2016 02 8 7 15
D 2016 03 12 0 12
A 2016 04 0 15 15
E 2016 04 34 0 34
A 2016 05 0 15 15
C 2016 08 0 89 89
B 2016 12 0 69 69
y
CTE(もっと意味のある名前を使用して自由に感じる!)、またそう、ランキングを追加します。それはまた、問題に予想される結果と一致していません2007年は1、2008年は2、2009年は3、201 6は4です。
r
再帰CTEは、前年度の名前/月データに基づいて、売上がゼロのダミー行と実際のデータを結合します。
再帰的なCTEによって生成される累積合計を分析して、開閉残高を追加することができます。これは、ウィンドウング句を使用して、どの販売値を含めるかを決定します。基本的には、開始時点と終了時点の残高はすべてこの時点までのすべての値の合計ですが、現在の行は含まれません。
サンプルデータに入力しましたか?あなたの出力データはA、2007、04、05を参照しますが、サンプルデータにはA、2007、04ではなくB、2007、05があります。これは2007年から現在までの年数か、データに存在する年数(例:2007年、2008年、2009年および2016年)ですか? P.S. **提示されたサンプルデータのために**ありがとうございます!この手軽な形式でデータを入手するのは稀です! * {: - D – Boneist
私はどこにB、2007,05を持っていません。S – bytebiscuit
'select 2 id、 'B' name、 '2007' year、 '05' month、デュアルユニオンからの2つの販売すべて – Boneist