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

SQL Server中Rollup关键字使用技巧createtable #t(a int,b int,c int,d int,e int)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,2,3,4,5)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,2,3,4,6)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,2,3,4,7)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,2,3,4,8)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,3,3,4,5)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,3,3,4,6)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,3,3,4,8)
SQL Server中Rollup关键字使用技巧
insertinto #t values(1,3,3,4,7)
SQL Server中Rollup关键字使用技巧
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,2,2,4,5)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,2,3,4,6)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,2,4,4,7)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,2,5,4,8)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,3,6,4,5)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,3,3,4,6)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,3,3,4,8)
SQL Server中Rollup关键字使用技巧
insertinto #t values(2,3,3,4,7)

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

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧
sum(b),sum(c),sum(d),sum(e) from #t groupby a with rollup


情况二:有多个分类汇总列,只需要一个合计.增加rollup之后,需要增加判断。

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧 b,
SQL Server中Rollup关键字使用技巧
sum(c),sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b with rollup
SQL Server中Rollup关键字使用技巧
havinggrouping(b)=0orgrouping(a)=1
SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧 b,
SQL Server中Rollup关键字使用技巧 c,
SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b,c with rollup
SQL Server中Rollup关键字使用技巧
havinggrouping(c)=0orgrouping(a)=1


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

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧
casewhengrouping(b)=1andgrouping(a)=0then'小计'elsecast(b asvarchar) end b,
SQL Server中Rollup关键字使用技巧
casewhengrouping(c)=1andgrouping(b)=0then'小计'elsecast(c asvarchar) end c,
SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b,c with rollup


另外一种显示小计的方式

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'
SQL Server中Rollup关键字使用技巧
whengrouping(b)=1thencast(a asvarchar)+'小计'
SQL Server中Rollup关键字使用技巧
elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧
casewhengrouping(b)=0andgrouping(c)=1
SQL Server中Rollup关键字使用技巧
thencast(b asvarchar)+'小计'elsecast(b asvarchar) end b,
SQL Server中Rollup关键字使用技巧
casewhengrouping(c)=1andgrouping(b)=0
SQL Server中Rollup关键字使用技巧
then''elsecast(c asvarchar) end c,
SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b,c with rollup


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

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧 b,
SQL Server中Rollup关键字使用技巧
casewhengrouping(c)=1andgrouping(b)=0then'小计'elsecast(c asvarchar) end c,
SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b,c with rollup
SQL Server中Rollup关键字使用技巧
havinggrouping(a)=1orgrouping(b)=0

 

SQL Server中Rollup关键字使用技巧selectcasewhengrouping(a)=1then'合计'elsecast(a asvarchar) end a,
SQL Server中Rollup关键字使用技巧
casewhengrouping(b)=1andgrouping(a)=0then'小计'elsecast(b asvarchar) end b,
SQL Server中Rollup关键字使用技巧 c,
SQL Server中Rollup关键字使用技巧
sum(d),sum(e) from #t
SQL Server中Rollup关键字使用技巧
groupby a,b,c with rollup
SQL Server中Rollup关键字使用技巧
havinggrouping(a)=1orgrouping(b)=1orgrouping(c)=0
 

相关文章:

  • 2021-12-03
  • 2021-10-15
  • 2022-01-11
  • 2022-02-06
  • 2021-11-24
  • 2021-10-07
猜你喜欢
  • 2021-06-06
  • 2022-02-20
  • 2021-09-06
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案