select case when grouping(姓名)=1 then ‘合计‘ else cast(姓名 as varchar) end 姓名, case when grouping(学期)=1 and grouping(姓名)=0 then ‘姓名小计‘ else cast(学期 as varchar) end 学期, case when cast(学期 as varchar)<>cast(‘姓名小计‘ AS varchar) and ISNULL(考次, ‘‘) = ‘‘ then ‘学期小计‘ else 考次 end as 考次, sum(语文) as 语文,sum(数学) as 数学,sum(英语) as 英语,sum(政治) as 政治 from tb group by 姓名,学期,考次 with rollup
T-SQL多个小计+合计,分类汇总,布布扣,bubuko.com
原文:http://www.cnblogs.com/ycdx2001/p/3850807.html