累积数据连同原始数据
Cumulative data along with the original data
我有两个 tables .
输入:
我加入了日历table并将数据更新到最新。
我需要一个输出。
我尝试使用 UNION 和聚合进行查询,但我需要查询两次并聚合相同的结果 table。由于 table 非常大。是否可以选择不同的方式
SELECT ID ,PERIOD,SUM(AMOUNTYTD) AMOUNTYTD,SUM(AMOUNT) AMOUNT
FROM (
SELECT ID ,b.PERIOD,SUM(AMOUNT) AMOUNTYTD,0 AMOUNT
FROM transaction a RIGHT OUTER JOIN CALENDAR b
ON b.PERIOD<=a.PERIOD
UNION ALL
SELECT ID ,PERIOD,0,SUM(AMOUNT)
FROM transaction
GROUP BY ID,PERIOD
)
GROUP BY ID,PERIOD
将定期金额与累计金额并排显示很容易 - 实际上,您只需要能够使用定期金额创建正确的 table,累计金额是分析求和的简单应用.
将日历 table 连接到“输入”数据的关键是使用 partitioned 外部连接 - 注意 partition by (id)
中的子句加入两个 table。这导致“输入”数据被划分为单独的子tables,每个不同的id
;日历 table 的外部连接是针对每个这样的子 table 单独完成的,然后结果与逻辑“联合所有”相结合。
with
input (id, period, amount) as (
select 1, 202010, 100 from dual union all
select 1, 202011, 50 from dual union all
select 2, 202011, 400 from dual
)
, calendar (period) as (
select 202010 from dual union all
select 202011 from dual union all
select 202012 from dual union all
select 202101 from dual
)
select id, period, amountytd, amount
from (
select i.id, period, i.amount,
sum(i.amount) over (partition by i.id order by period)
as amountytd
from calendar c left outer join input i partition by (id)
using (period)
)
where amountytd is not null
order by id, period
;
ID PERIOD AMOUNTYTD AMOUNT
--- ---------- ---------- ----------
1 202010 100 100
1 202011 150 50
1 202012 150
1 202101 150
2 202011 400 400
2 202012 400
2 202101 400
您有查询 - I have joined with the calendar table and bring the data till current.
让我们假设它是 your_query
您可以在其上使用分析函数,如下所示:
Select t.*,
Case when lead(amountytd) over (partition by id order by period) = amountytd
then null
else amountytd
end as amount
From (your_query) t
我有两个 tables .
输入:
我加入了日历table并将数据更新到最新。
我需要一个输出。
我尝试使用 UNION 和聚合进行查询,但我需要查询两次并聚合相同的结果 table。由于 table 非常大。是否可以选择不同的方式
SELECT ID ,PERIOD,SUM(AMOUNTYTD) AMOUNTYTD,SUM(AMOUNT) AMOUNT
FROM (
SELECT ID ,b.PERIOD,SUM(AMOUNT) AMOUNTYTD,0 AMOUNT
FROM transaction a RIGHT OUTER JOIN CALENDAR b
ON b.PERIOD<=a.PERIOD
UNION ALL
SELECT ID ,PERIOD,0,SUM(AMOUNT)
FROM transaction
GROUP BY ID,PERIOD
)
GROUP BY ID,PERIOD
将定期金额与累计金额并排显示很容易 - 实际上,您只需要能够使用定期金额创建正确的 table,累计金额是分析求和的简单应用.
将日历 table 连接到“输入”数据的关键是使用 partitioned 外部连接 - 注意 partition by (id)
中的子句加入两个 table。这导致“输入”数据被划分为单独的子tables,每个不同的id
;日历 table 的外部连接是针对每个这样的子 table 单独完成的,然后结果与逻辑“联合所有”相结合。
with
input (id, period, amount) as (
select 1, 202010, 100 from dual union all
select 1, 202011, 50 from dual union all
select 2, 202011, 400 from dual
)
, calendar (period) as (
select 202010 from dual union all
select 202011 from dual union all
select 202012 from dual union all
select 202101 from dual
)
select id, period, amountytd, amount
from (
select i.id, period, i.amount,
sum(i.amount) over (partition by i.id order by period)
as amountytd
from calendar c left outer join input i partition by (id)
using (period)
)
where amountytd is not null
order by id, period
;
ID PERIOD AMOUNTYTD AMOUNT
--- ---------- ---------- ----------
1 202010 100 100
1 202011 150 50
1 202012 150
1 202101 150
2 202011 400 400
2 202012 400
2 202101 400
您有查询 - I have joined with the calendar table and bring the data till current.
让我们假设它是 your_query
您可以在其上使用分析函数,如下所示:
Select t.*,
Case when lead(amountytd) over (partition by id order by period) = amountytd
then null
else amountytd
end as amount
From (your_query) t