入力し、コードを使用してクエリでゼロ値のレコードを追加:は累積的な分析機能に
with data as (
select 1 id, 'A' name, 'fruit' r_group, '2007' year, '04' month, 5 sales from dual union all
select 2 id, 'Z' name, 'fruit' r_group, '2007' year, '04' month, 99 sales from dual union all
select 3 id, 'A' name, 'fruit' r_group, '2008' year, '05' month, 10 sales from dual union all
select 4 id, 'B' name, 'vegetable' r_group, '2008' year, '07' month, 20 sales from dual
)
select year,
month,
r_group,
sum(sales) sales,
sum(opening) opening,
sum(closing) closing
from (
select t.*,
(sum(sales) over (partition by name, r_group
order by year, month
rows between unbounded preceding and current row
) -sales) as opening,
sum(sales) over (partition by name, r_group
order by year, month
rows between unbounded preceding and current row
) as closing
from data t
)
group by year, month, r_group
order by year, month
出力:
year | month | r_group | sales | opening | closing |
2007 | 04 | fruit | 104 | 0 | 104 |
2008 | 05 | fruit | 10 | 104 | 114 |
2008 | 07 | vegetable | 20 | 0 | 20 |
:
year | month | r_group | sales | opening | closing |
2007 | 04 | fruit | 104 | 0 | 104 |
2008 | 05 | fruit | 10 | 5 | 15 |
2008 | 07 | vegetable | 20 | 0 | 20 |
は、私は、出力は次のようになりたいです
私は、月= 05のデータにゼロ値レコードを追加し、tのようにname = 'Z'彼自身:
ただし、データを編集することなく選択クエリの一部としてこれを実行できるかどうかを知りたいと思います。
EDIT
内側のselect文になるデータベーステーブルへの入力詳細版:年、月、名前、r_group、オープニング、クロージングを。 (サードパーティを
select t.*,
(sum(sales) over (partition by name, r_group
order by year, month
rows between unbounded preceding and current row
) -sales) as opening,
sum(sales) over (partition by name, r_group
order by year, month
rows between unbounded preceding and current row
) as closing
from data t
が、私は分析ツールを使用して、その上で集計を使用します:つまり、このクエリの結果は、dbテーブルを移入して、その後どうなるの外側のクエリを使用して凝集するために使用されます)を指定すると、r_groupに集約されます。しかし、年、月、名前、r_groupの詳細はバックグラウンドに存在しなければなりません。他のworkdsで
EDIT 2
、私は動的に不足しているデータを追加しようとしています。たとえば、name = 'Z'が2007,04に存在していても2008,05にない場合、累積関数は2008になると失敗します。それは失敗する。
SELECT t.*,
SUM(sales) OVER (PARTITION BY r_group, dt) AS r_group_month_sales,
COALESCE(
SUM(sales) OVER (
PARTITION BY r_group
ORDER BY dt
RANGE BETWEEN UNBOUNDED PRECEDING AND INTERVAL '1' MONTH PRECEDING
),
0
) AS opening,
SUM(sales) OVER (
PARTITION BY r_group
ORDER BY dt
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS closing
FROM (
SELECT d.*,
TO_DATE(year || month, 'YYYYMM') AS dt
FROM data d
) t
ORDER BY dt
:これも出力の名前が含まれます
:
SELECT t.*,
SUM(sales) OVER (PARTITION BY r_group ORDER BY year, month) - sales
AS opening,
SUM(sales) OVER (PARTITION BY r_group ORDER BY year, month) AS closing
FROM (
SELECT r_group,
year,
month,
SUM(sales) AS sales
FROM data
GROUP BY r_group, year, month
) t
ORDER BY year, month
更新:
気にする列のロジックについて説明できますか? –
for r_group = 'fruit' month = '05 'の開始値は、month =' 04 'の終了値でなければなりません。 r_group = 'fruit'の月= '05'の締め値は、月= '04'からの開始値+月= '05'の売上値でなければなりません。 – bytebiscuit
この質問は非常に混乱しています.2つの答えがあり、正確な予測出力が得られましたが、その結果に興味はないようですが、集計前の中間ステップで表示されます。あなたはそれがあなたが期待していることを再考し、**のために[MCVE]を生成しなければなりません**(つまり、集計前にステップから何かを求めて、そのステップの期待出力を書き出し、 *その後のステップのために)。おそらく、この質問を放棄/削除し、新しい(より明確な)質問をする必要があります。 – MT0