转载自:http://www.cnblogs.com/yiway/archive/2007/11/12/957281.html

首先创建测试表、添加数据。

【转载】SQL Server中Rollup关键字使用技巧create table #t(a int,b int,c int,d int,e int)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,2,3,4,5)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,2,3,4,6)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,2,3,4,7)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,2,3,4,8)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,3,3,4,5)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,3,3,4,6)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,3,3,4,8)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(1,3,3,4,7)
【转载】SQL Server中Rollup关键字使用技巧
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,2,2,4,5)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,2,3,4,6)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,2,4,4,7)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,2,5,4,8)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,3,6,4,5)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,3,3,4,6)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,3,3,4,8)
【转载】SQL Server中Rollup关键字使用技巧
insert into #t values(2,3,3,4,7)

情况一:只有一个分类汇总列时,只需要一个合计。只需要增加with rollup即可。

【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧
sum(b),sum(c),sum(d),sum(e) from #t group by a with rollup

情况二:有多个分类汇总列,只需要一个合计.增加rollup之后,需要增加判断。
【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    b,
【转载】SQL Server中Rollup关键字使用技巧
sum(c),sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b with rollup 
【转载】SQL Server中Rollup关键字使用技巧
having grouping(b)=0 or grouping(a)=1
【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    b,
【转载】SQL Server中Rollup关键字使用技巧    c,
【转载】SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b,c with rollup 
【转载】SQL Server中Rollup关键字使用技巧
having grouping(c)=0 or grouping(a)=1

情况三:有多个分类汇总列,需要全部的小计和合计
【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varcharend b,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varcharend c,
【转载】SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b,c with rollup 

另外一种显示小计的方式

【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' 
【转载】SQL Server中Rollup关键字使用技巧    
when grouping(b)=1 then cast(a as varchar)+'小计'
【转载】SQL Server中Rollup关键字使用技巧    
else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(b)=0 and grouping(c)=1 
【转载】SQL Server中Rollup关键字使用技巧    
then cast(b as varchar)+'小计' else cast(b as varcharend b,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(c)=1 and grouping(b)=0 
【转载】SQL Server中Rollup关键字使用技巧    
then '' else cast(c as varcharend c,
【转载】SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b,c with rollup 


情况四:有多个分类汇总列,需要部分的小计和合计

【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    b,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varcharend c,
【转载】SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b,c with rollup 
【转载】SQL Server中Rollup关键字使用技巧
having grouping(a)=1 or grouping(b)=0

 

【转载】SQL Server中Rollup关键字使用技巧select case when grouping(a)=1 then '合计' else cast(a as varcharend a,
【转载】SQL Server中Rollup关键字使用技巧    
case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varcharend b,
【转载】SQL Server中Rollup关键字使用技巧    c,
【转载】SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t 
【转载】SQL Server中Rollup关键字使用技巧
group by a,b,c with rollup 
【转载】SQL Server中Rollup关键字使用技巧
having grouping(a)=1 or grouping(b)=1 or grouping(c)=0

相关文章: