在每种类型的记录结束后显示“总计”

时间:2019-04-02 17:40:54

标签: sql-server

我有一个包含两列的表,即。 1. ClassName 2.学生姓名

enter image description here

1 个答案:

答案 0 :(得分:2)

使用 ROLLUP 并将联合添加到表格中

declare @mytable as table(classname varchar(100),studentname varchar(100))

insert into @mytable values
('10th','Hakim'),
('10th','Fathi'),
('9th','Saber'),
('9th','Wahid'),
('9th','Isamïl')

select * from 
(select classname,total=convert(varchar(100),count(1)) from @mytable
GROUP BY ROLLUP(classname)
union
select * from @mytable) v
where classname is not null
order by classname,isnumeric(total) 

Roollup sql server script