-2

我有一组数据,我想在每个时期之后求和。例如:

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

那可能吗?

我想这样做是因为我有很多数据,我每个月都会更新这些数据,因此我不想在每次更新数据时都计算这些总和 - 我不可能每个月都在不同的工作表中!(由管理层决定)

4

2 回答 2

0

你应该使用rollup.

尝试这个:

select period, vessel_name, sum(total_amount) from table group by rollup(period, vessel_name)
于 2013-10-30T11:03:48.680 回答
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;
于 2013-10-30T11:44:33.717 回答