T-SQL多个小计+合计,分类汇总

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多个小计+合计,分类汇总

 

 

T-SQL多个小计+合计,分类汇总

上一篇:Oracle查看用户所在表空间


下一篇:Mysql 常用函数(16)- locate 函数