新年好,请教with rollup grouping的用法???

samyou 2013-02-19 12:07:23
1.表:
create table #test(dept varchar(20),name varchar(20),country varchar(20),play varchar(20),age int,score int)
insert into #test select '技术部','张一','中国','足球',28,120
insert into #test select '技术部','李一','中国','篮球',28,120
insert into #test select '生产部','张二','中国','足球',28,120
insert into #test select '生产部','李二','英国','羽毛球',28,120
2.请问按照dept进行分组小计,最后合计,要求要显示所有字段,在dept列显示小计、合计,用with rollup 是怎样进行编写的啊?
3.select 时,想增加一个字段,根据记录从1自动递增起,用什么语句呢?
...全文
174 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
samyou 2013-02-19
  • 打赏
  • 举报
回复
多谢回复,我想问的是根据dept进行小计、合计,并且小计、合计字符要显示在dept列,只对score统计,并且要显示所有列名。 我发觉如果表有多个列时,要显示所有列,得出来的结果不行。
-狙击手- 2013-02-19
  • 打赏
  • 举报
回复
select case when grouping(dept) =1 then '合计' else dept end , case when grouping(country)=1 then case when grouping(dept) =0 then '小计' else '' end else country end, avg(age),avg(score) from #test group by dept,country with rollup 至于增加自增列,2005及以上版本直接用排名函数就行
samyou 2013-02-19
  • 打赏
  • 举报
回复
附加说明:是对score进行统计。
samyou 2013-02-19
  • 打赏
  • 举报
回复
select case when grouping(dept) =1 then '合计' when name is null then '小计' else dept end dept,name, country,play,age age,sum(score) from #test group by dept,name,country,play,age with rollup 我用了这些语句可以得出小计和合计,但多了好多不相关的记录出来,怎样解决呢?
samyou 2013-02-19
  • 打赏
  • 举报
回复
排名函数都需要进行order ,我只想根据原来记录的基础上,进行自动增1.

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧