关于按月合计以及累计的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
或者用
union all
[其他解释]
这是结果还是原数据?
[其他解释]
group by ... with rollup
[其他解释]
不懂,关注一下。
[其他解释]
把数据,结果贴出来..
[其他解释]
思路:
分别统计出1月的 和2月的 然后nuion all
[其他解释]
是union all
[其他解释]
group by + with rollup
[其他解释]
我给的是结果数据集
[其他解释]
大家需要的原始~~
[其他解释]
up
[其他解释]
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
[其他解释]
很明显我给分的两位已经符合我问题的要求。
谢谢!!!!!
[其他解释]
看看,顶一下