关于按月合计以及累计的SQL该如何实现?
业务员年月客户 发货运费质保金
张三20091香港某某公司1
张三20091上海某某公司1
张三20091厦门某某公司1
张三20091当月合计 3
张三20091累 计 3
张三20092厦门某某公司2
张三20092上海某某公司2
张三20092珠海某某公司2
张三20092当月合计 6
张三20092累 计 9
==============================================================================
明细表汇总出上面的效果:
按月合计这个好办,但是累计似乎比较难。希望有做过类似应用的兄弟不吝赐教
[最优解释]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([业务员] varchar(10),[年] int,[月] int,[客户] varchar(20),[发货] int)
insert [tb] select '张三',2009,1,'香港某某公司',1
union all select '张三',2009,1,'上海某某公司',1
union all select '张三',2009,1,'厦门某某公司',1
--union all select '张三',2009,1,'当月合计',3
--union all select '张三',2009,1,'累计',3
union all select '张三',2009,2,'厦门某某公司',2
union all select '张三',2009,2,'上海某某公司',2
union all select '张三',2009,2,'珠海某某公司',2
--union all select '张三',2009,2,'当月合计',6
--union all select '张三',2009,2,'累计',9
go
select *
from
(
select * from tb
union all
select 业务员,年,月,'当月合计',sum(发货) from tb group by 业务员,年,月
union all
select 业务员,年,月,'累计',(select sum(发货) from (select 业务员,年,月,sum(发货) 发货 from tb group by 业务员,年,月) a where a.业务员=t.业务员 and a.年=t.年 and a.月<=t.月) from (select 业务员,年,月,sum(发货) 发货 from tb group by 业务员,年,月) t group by 业务员,年,月
) tb
order by 年,月,case 客户 when '累计' then 3 when '当月合计' then 2 else 1 end
/*
业务员 年 月 客户 发货
---------- ----------- ----------- -------------------- -----------
张三 2009 1 香港某某公司 1
张三 2009 1 上海某某公司 1
张三 2009 1 厦门某某公司 1
张三 2009 1 当月合计 3
张三 2009 1 累计 3
张三 2009 2 厦门某某公司 2
张三 2009 2 上海某某公司 2
张三 2009 2 珠海某某公司 2
张三 2009 2 当月合计 6
张三 2009 2 累计 9
(10 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([業務員] varchar(10),[年] int,[月] int,[客戶] varchar(20),[發貨] int)
insert [tb] select 'A',2009,1,'C1',1
union all select 'A',2009,1,'C2',1
union all select 'A',2009,1,'C3',1
union all select 'A',2009,2,'C3',2
union all select 'A',2009,2,'C2',2
union all select 'A',2009,2,'C4',2
union all select 'A',2009,3,'C5',2
union all select 'A',2009,3,'C6',2
union all select 'A',2009,3,'C4',2
union all select 'B',2009,2,'C3',2
union all select 'B',2009,2,'C2',2
union all select 'B',2009,2,'C4',2
union all select 'B',2009,2,'C3',2
union all select 'B',2009,5,'C2',2
union all select 'B',2009,5,'C4',2
;with play
as (
select [業務員],[年],[月],'當月統計' as [客戶],sum([發貨]) as [發貨]
from tb group by [業務員],[年],[月]
)
select [業務員],[年],[月],[客戶],[發貨]
from
(
select * ,1 as temp from tb
union all
select * ,2 as temp from play
union all
select [業務員],[年],[月],'累 計',
( select sum([發貨]) from play where [業務員]=A.[業務員] and ([年]<A.[年] or ([年]=A.[年] and [月]<=A.[月])))
,3 as temp
from play A
) X
order by [業務員],[年],[月],temp
/*
業務員 年 月 客戶 發貨
---------- ----------- ----------- -------------------- -----------
A 2009 1 C1 1
A 2009 1 C2 1
A 2009 1 C3 1
A 2009 1 當月統計 3
A 2009 1 累 計 3
A 2009 2 C3 2
A 2009 2 C2 2
A 2009 2 C4 2
A 2009 2 當月統計 6
A 2009 2 累 計 9
A 2009 3 C5 2
A 2009 3 C6 2
A 2009 3 C4 2
A 2009 3 當月統計 6
A 2009 3 累 計 15
B 2009 2 C3 2
B 2009 2 C2 2
B 2009 2 C4 2
B 2009 2 C3 2
B 2009 2 當月統計 8
B 2009 2 累 計 8
B 2009 5 C2 2
B 2009 5 C4 2
B 2009 5 當月統計 4
B 2009 5 累 計 12
*/
drop table tb
group by ... with rollup
group by ... with rollup
[其他解释]
select 业务员, 年 ,月 ,客户, 发货
from Tb
union all
select 业务员, 年 ,月 ,'当月合计' ,sum(发货)
from Tb
group by 业务员, 年 ,月
union all
select 业务员, 年 ,月 ,'累 计' ,
(select sum(发货) from Tb as A where A.业务员=Tb.业务员
and A.年<Tb.年
or (A.年=Tb.年 and A.月<=Tb.月))
group by 业务员, 年 ,月
Order by 业务员, 年 ,月, case when 客户='当月合计' then 2
when 客户='累 计' then 3
else 1 end