您的位置:首页 > 博客中心 > 数据库 >

sql中 with rollup 、with cube、grouping 统计函数用法

时间:2022-03-10 18:05

with rollup 、with cube、grouping
CUBE 和 ROLLUP 之间的区别在于:
CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
grouping:
当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。

--------------------------------------------------------------------------------

首先创建测试表、添加数据。
table #t(a int,b int,c int,d int,e int)
gxlsystem.com,布布扣insert into #t values(1,2,3,4,5)
gxlsystem.com,布布扣insert into #t values(1,2,3,4,6)
gxlsystem.com,布布扣insert into #t values(1,2,3,4,7)
gxlsystem.com,布布扣insert into #t values(1,2,3,4,8)
gxlsystem.com,布布扣insert into #t values(1,3,3,4,5)
gxlsystem.com,布布扣insert into #t values(1,3,3,4,6)
gxlsystem.com,布布扣insert into #t values(1,3,3,4,8)
gxlsystem.com,布布扣insert into #t values(1,3,3,4,7)
gxlsystem.com,布布扣
gxlsystem.com,布布扣insert into #t values(2,2,2,4,5)
gxlsystem.com,布布扣insert into #t values(2,2,3,4,6)
gxlsystem.com,布布扣insert into #t values(2,2,4,4,7)
gxlsystem.com,布布扣insert into #t values(2,2,5,4,8) insert into #t values(2,3,6,4,5) insert into #t values(2,3,3,4,6) insert into #t values(2,3,3,4,8) insert into #t values(2,3,3,4,7)

情况一:只有一个分类汇总列时,只需要一个合计。只需要增加with rollup即可。
case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣sum(b),sum(c),sum(d),sum(e) from #t group by a with rollup


情况二:有多个分类汇总列,只需要一个合计.增加rollup之后,需要增加判断。
case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣    b,
gxlsystem.com,布布扣sum(c),sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b with rollup 
gxlsystem.com,布布扣having grouping(b)=0 or grouping(a)=1

case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣    b,
gxlsystem.com,布布扣    c,
gxlsystem.com,布布扣sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b,c with rollup 
gxlsystem.com,布布扣having grouping(c)=0 or grouping(a)=1


情况三:有多个分类汇总列,需要全部的小计和合计。
case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣    case when grouping(b)=1 and grouping(a)=0 then ‘小计‘ else cast(b as varchar) end b,
gxlsystem.com,布布扣    case when grouping(c)=1 and grouping(b)=0 then ‘小计‘ else cast(c as varchar) end c,
gxlsystem.com,布布扣sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b,c with rollup 


另外一种显示小计的方式
case when grouping(a)=1 then ‘合计‘ 
gxlsystem.com,布布扣    when grouping(b)=1 then cast(a as varchar)+‘小计‘
gxlsystem.com,布布扣    else cast(a as varchar) end a,
gxlsystem.com,布布扣    case when grouping(b)=0 and grouping(c)=1 
gxlsystem.com,布布扣    then cast(b as varchar)+‘小计‘ else cast(b as varchar) end b,
gxlsystem.com,布布扣    case when grouping(c)=1 and grouping(b)=0 
gxlsystem.com,布布扣    then ‘‘ else cast(c as varchar) end c,
gxlsystem.com,布布扣sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b,c with rollup 


情况四:有多个分类汇总列,需要部分的小计和合计
case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣    b,
gxlsystem.com,布布扣    case when grouping(c)=1 and grouping(b)=0 then ‘小计‘ else cast(c as varchar) end c,
gxlsystem.com,布布扣sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b,c with rollup 
gxlsystem.com,布布扣having grouping(a)=1 or grouping(b)=0


case when grouping(a)=1 then ‘合计‘ else cast(a as varchar) end a,
gxlsystem.com,布布扣    case when grouping(b)=1 and grouping(a)=0 then ‘小计‘ else cast(b as varchar) end b,
gxlsystem.com,布布扣    c,
gxlsystem.com,布布扣sum(d),sum(e) from #t 
gxlsystem.com,布布扣group by a,b,c with rollup 
gxlsystem.com,布布扣having grouping(a)=1 or grouping(b)=1 or grouping(c)=0

引用:


sql中 with rollup 、with cube、grouping 统计函数用法,布布扣,bubuko.com

热门排行

今日推荐

热门手游