34,591
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select '一' as id union all
select '二' as id union all
select '三' as id
)tp order by id
drop table tb
create table tb(编号 int, 班级 varchar(10),语文 int,数学 int )
insert into tb select 1,'一班',80,90
insert into tb select 2,'一班',80,90
insert into tb select 3,'一班',60,90
insert into tb select 4,'三班',70,70
insert into tb select 5,'三班',80,90
insert into tb select 6,'三班',80,80
insert into tb select 7,'二班',90,80
insert into tb select 8,'二班',90,60
insert into tb select 9,'一班',80,90
select
case
when grouping(编号)+grouping(班级)=1 then '小计'
when grouping(编号)+grouping(班级)=2 then '合计'
else ltrim(编号) end as 编号,班级,sum( 语文) ,sum(数学)
from tb
group by
班级,编号
with rollup
order by
case when left(班级,1)='一' then 0
when left(班级,1)='二' then 1
else 2 end
declare @t table(编号 int,班级 varchar(10),语文 int,数学 int)
insert into @t select 1,'一班',80,90
insert into @t select 2,'一班',80,90
insert into @t select 3,'一班',60,90
insert into @t select 4,'三班',70,70
insert into @t select 5,'三班',80,90
insert into @t select 6,'三班',80,80
insert into @t select 7,'二班',90,80
insert into @t select 8,'二班',90,60
insert into @t select 9,'一班',80,90
select 编号,班级=case when isnull(编号,'')='' and 班级<>'' then '小计' when isnull(编号,'')='' and isnull(班级,'')='' then '合计' else 班级 end,
语文,数学 from
(select *,px=1 from @t
union all
select null,班级,sum(语文),sum(数学),px=2 from @t group by 班级
union all
select null,null,sum(语文),sum(数学),px=3 from @t)a
order by case 班级 when '一班' then 0 when '二班' then 1 when '三班' then 2 else 3 end,px
drop table tb
create table tb(编号 int, 班级 varchar(10),语文 int,数学 int )
insert into tb select 1,'一班',80,90
insert into tb select 2,'一班',80,90
insert into tb select 3,'一班',60,90
insert into tb select 4,'三班',70,70
insert into tb select 5,'三班',80,90
insert into tb select 6,'三班',80,80
insert into tb select 7,'二班',90,80
insert into tb select 8,'二班',90,60
insert into tb select 9,'一班',80,90
select
case
when grouping(编号)+grouping(班级)=2 then '合计'
when grouping(编号)+grouping(班级)=1 then '小计'
else ltrim(编号) end as 编号,班级,sum( 语文) ,sum(数学)
from tb
group by
班级,编号
with rollup
create table tb(班级 varchar(10), 语文 int , 数学 int)
insert into tb values('一班', 80, 90 )
insert into tb values('一班', 80, 90 )
insert into tb values('一班', 60, 90 )
insert into tb values('三班', 70, 70 )
insert into tb values('三班', 80, 90 )
insert into tb values('三班', 80, 80 )
insert into tb values('二班', 90, 80 )
insert into tb values('二班', 90, 60 )
insert into tb values('一班', 80, 90 )
go
select * from
(
select * from tb
union all
select 班级 + '小计' 班级, sum(语文) 语文, sum(数学) 数学 from tb group by 班级 + '小计'
union all
select '合计' 班级, sum(语文) 语文, sum(数学) 数学 from tb
) t
order by case 班级 when '合计' then 2 else 1 end , 班级
drop table tb
/*
班级 语文 数学
-------------- ----------- -----------
二班 90 80
二班 90 60
二班小计 180 140
三班 70 70
三班 80 90
三班 80 80
三班小计 230 240
一班 80 90
一班 80 90
一班 60 90
一班 80 90
一班小计 300 360
合计 710 740
(所影响的行数为 13 行)
*/
有一张表A:
科目编号 科目名称 金额 部门编号
0001 科目一 100.00 BM0001
0003 科目三 200.00 BM0001
0002 科目二 300.00 BM0001
0001 科目一 400.00 BM0002
0003 科目三 500.00 BM0002
一条SQL实现:按部门分组并计算合计,并每个部门内按科目编号排序
如下:
科目编号 金额
0001 100.00
0002 300.00
0003 200.00
BM0001合计 600.00
0001 400.00
0003 500.00
BM0002合计 900.00
declare @tb table (科目编号 varchar(10), 科目名称 varchar(10), 金额 decimal(10,2), 部门编号 varchar(10))
insert into @tb select '0001','科目一',100.00,'BM0001'
insert into @tb select '0003','科目三',200.00,'BM0001'
insert into @tb select '0002','科目二',300.00,'BM0001'
insert into @tb select '0001','科目一',400.00,'BM0002'
insert into @tb select '0003','科目三',500.00,'BM0002'
select
--case when grouping(部门编号)=1 then '总计' else 部门编号 end as 部门编号 ,
case when grouping(科目编号)= 1 then 部门编号+'合计' else 科目编号 end as 科目编号,
sum(金额) as 金额
from @tb
group by 部门编号,科目编号
with rollup
having grouping(部门编号)+grouping(科目编号)<> 2
/*
科目编号 金额
-------------- ----------------------------------------
0001 100.00
0002 300.00
0003 200.00
BM0001合计 600.00
0001 400.00
0003 500.00
BM0002合计 900.00
*/