如何给表添加合计(每组都添加)

qq_41715427 2018-06-29 10:15:01
select c.collegeNo, c.CollegeName 系别,SpeGrade 年级,count(distinct ClassNo) 班数,
count(1)人数,count(case when sex='1' then 1 end ) 男,count(case when sex='0' then 1 end ) 女,
count(case when Nation='MZDM002' then 1 end) 满族,
count(case when Nation='MZDM004' then 1 end) 回族,
count(case when Nation='MZDM009' then 1 end) 蒙族,
count(d.StudentId) 困难人数,
count(case when Nation not in ('MZDM009','MZDM002','MZDM004','MZDM001') then 1 end) 其他族
from StudentInfo s inner join CollegeInfo c on s.CollegeNo=c.CollegeNo left join [dbo].[Need_NeedData] d on s.studentId=d.StudentId
group by c.collegeNo,s.CollegeNo,c.CollegeName,SpeGrade

查询结果如下:


想要每个系增加合计,如下图

应该怎么做
...全文
913 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaodai511 2018-07-05
  • 打赏
  • 举报
回复
再union all 一个按系别分组 然后sum sum sum sum 。。。的表呢
zjcxc 2018-07-04
  • 打赏
  • 举报
回复
select c.collegeNo, c.CollegeName 系别,
case grouping(SpeGrade) when 1 then N'合计' else SpeGrade end 年级,count(distinct ClassNo) 班数,
count(1)人数,count(case when sex='1' then 1 end ) 男,count(case when sex='0' then 1 end ) 女,
count(case when Nation='MZDM002' then 1 end) 满族,
count(case when Nation='MZDM004' then 1 end) 回族,
count(case when Nation='MZDM009' then 1 end) 蒙族,
count(d.StudentId) 困难人数,
count(case when Nation not in ('MZDM009','MZDM002','MZDM004','MZDM001') then 1 end) 其他族
from StudentInfo s inner join CollegeInfo c on s.CollegeNo=c.CollegeNo left join [dbo].[Need_NeedData] d on s.studentId=d.StudentId
group by c.collegeNo,s.CollegeNo,c.CollegeName,SpeGrade with rollup
having grouping(c.CollegeName)=0
卖水果的net 2018-07-03
  • 打赏
  • 举报
回复
在前端用程序实现,SQL 的结果,只是二维表,没有合并单元格的功能。
shinger126 2018-07-03
  • 打赏
  • 举报
回复
这个用报表工具比较好实现,用SQL实现这样的效果比较为难
sywcf 2018-07-03
  • 打赏
  • 举报
回复
单纯的语句不太好整,你写个存储过程中,建个临时表,然后分组插入一条合计记录.查询结果时,排下序就行了
RINK_1 2018-06-29
  • 打赏
  • 举报
回复
group by+with rollup
qq_41715427 2018-06-29
  • 打赏
  • 举报
回复
引用 1 楼 baidu_36457652 的回复:
Excel表比较好弄

没明白怎么弄啊,想拼接html标签,实现动态加载
  • 打赏
  • 举报
回复
Excel表比较好弄

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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