以下的文章主要描述的是SQL Server Rollup关键字的具体使用技巧,在其实际操作中我们首先是创建测试表、添加数据,以下就是文章的主要内容的详细描述,望大家在浏览之后会对其有更深的了解。
创新互联建站是专业的金口河网站建设公司,金口河接单;提供成都网站制作、网站设计、外贸网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行金口河网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
- create table #t(a int,b int,c int,d int,e int)
- insert into #t values(1,2,3,4,5)
- insert into #t values(1,2,3,4,6)
- insert into #t values(1,2,3,4,7)
- insert into #t values(1,2,3,4,8)
- insert into #t values(1,3,3,4,5)
- insert into #t values(1,3,3,4,6)
- insert into #t values(1,3,3,4,8)
- insert into #t values(1,3,3,4,7)
- insert into #t values(2,2,2,4,5)
- insert into #t values(2,2,3,4,6)
- insert into #t values(2,2,4,4,7)
- 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即可。
- select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- sum(b),sum(c),sum(d),sum(e) from #t group by a with rollup
情况二:有多个分类汇总列,只需要一个合计.增加SQL Server rollup之后,需要增加判断。
- select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- b,
- sum(c),sum(d),sum(e) from #t
- group by a,b with rollup
- having grouping(b)=0 or grouping(a)=1select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- b,
- c,
- sum(d),sum(e) from #t
- group by a,b,c with rollup
- having grouping(c)=0 or grouping(a)=1
情况三:有多个分类汇总列,需要全部的小计和合计。
- select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varchar) end b,
- case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varchar) end c,
- sum(d),sum(e) from #t
- group by a,b,c with rollup
另外一种显示小计的方式
- select case when grouping(a)=1 then '合计'
- when grouping(b)=1 then cast(a as varchar)+'小计'
- else cast(a as varchar) end a,
- case when grouping(b)=0 and grouping(c)=1
- then cast(b as varchar)+'小计' else cast(b as varchar) end b,
- case when grouping(c)=1 and grouping(b)=0
- then '' else cast(c as varchar) end c,
- sum(d),sum(e) from #t
- group by a,b,c with rollup
情况四:有多个分类汇总列,需要部分的小计和合计
- select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- b,
- case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varchar) end c,
- sum(d),sum(e) from #t
- group by a,b,c with rollup
- having grouping(a)=1 or grouping(b)=0
- select case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
- case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varchar) end b,
- c,
- sum(d),sum(e) from #t
- group by a,b,c with rollup
- having grouping(a)=1 or grouping(b)=1 or grouping(c)=0
上述的相关内容就是对SQL Server Rollup关键字使用技巧的描述,希望会给你带来一些帮助在此方面。
当前名称:SQLServerRollup关键字的使用窍门
网页链接:http://www.shufengxianlan.com/qtweb/news3/38253.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联