我有一组数据,我希望在每个时期之后求和。例如:
Period Vessel_name Total_amount
1301 Vessel_a 1000.00
1301 Vessel_b 4000.00
1302 Vessel_c 3000.00
1302 Vessel_d 5000.00
1302 Vessel_e 2000.00
我想要这个结果:
Period Vessel_name Total_amount
1301 Vessel_a 1000.00
1301 Vessel_b 4000.00
Total period 5000.00
1302 Vessel_c 3000.00
1302 Vessel_d 5000.00
1302 Vessel_e 2000.00
Total Period 10,000,00
这可能吗?
我想这样做是因为我有很多数据,我每月更新一次,因此每次更新数据时我都不想这些数据 - 我不可能每个月都有这样的数据在不同的床单! (由管理层决定)
答案 0 :(得分:0)
你应该使用rollup
。
试试这个:
select period, vessel_name, sum(total_amount) from table group by rollup(period, vessel_name)
答案 1 :(得分:0)
使用汇总来获取子总和。使用case语句获取子总和的描述性文本。使用必须摆脱整体总数。
select
case when Vessel_name is null then 'Total' else to_char(Period) end as period,
case when Vessel_name is null then 'period' else Vessel_name end as vessel_name
, sum(Total_amount)
from the_table
group by rollup(Period, Vessel_name)
having Period is not null;