0

在下面的查询中,我怎样才能只做一个查询来给我结果,而不是用 diff 分组制作副本并将它们联合起来?

如果可能的话。

提前致谢!!

`create table #temp1 (col1 varchar(50), col2 varchar(50), col3 varchar(50), col4 varchar(50), col5 varchar(50), sumit int)

insert into #temp1 values('AEAMS','CE Europe', 'Belarus', 'Govt', 'Int Fed Gvt', 1)
insert into #temp1 values('AEAMS','CE Europe', 'Belarus', 'Govt', 'Public Lib', 1)
insert into #temp1 values('AEDS','Japan', 'Japan C', 'Acad', 'CollUnive', 1)
insert into #temp1 values('AEDS','Japan', 'Japan F', 'Acad', 'Med', 1)
insert into #temp1 values('A- Regular Databases','UK and Ireland', 'Ireland', 'School', 'HIGH SCHOOL', 1)


Select col1 CC, null GM, null Terr, null Mkt, null Seg,  sum(sumit) SS
from #temp1
group by col1

Union

Select col1 CC, col2 GM, null Terr, null Mkt, null Seg,  sum(sumit) SS
from #temp1
group by col1, col2

Union

Select col1 CC, col2 GM, col3 Terr, null Mkt, null Seg,  sum(sumit) SS
from #temp1
group by col1, col2, col3

Union

Select col1 CC, col2 GM, col3 Terr, col4 Mkt, null Seg,  sum(sumit) SS
from #temp1
group by col1, col2, col3, col4, col5
4

1 回答 1

4

尝试使用WITH ROLLUP

Select col1 CC, col2 GM, col3 Terr, col4 Mkt, null Seg,  sum(sumit) SS
from #temp1
group by col1, col2, col3, col4, col5
with rollup

SQL 小提琴示例

于 2012-08-09T23:47:57.377 回答