金蝶K3月资金回笼分析表

按照部门业务员客户汇总出年度的各个月份的资金回笼情况

 

CREATE proc stkd_月资金回笼分析表
@year int
as

set nocount on

create table #aa(
fdept 
varchar(50),
femp 
varchar(50),
fcust 
varchar(80),
f1 
decimal default(0),
f2 
decimal default(0),
f3 
decimal default(0),
f4 
decimal default(0),
f5 
decimal default(0),
f6 
decimal default(0),
f7 
decimal default(0),
f8 
decimal default(0),
f9 
decimal default(0),
f10 
decimal default(0),
f11 
decimal default(0),
f12 
decimal default(0),
l1 
decimal default(0),
l2 
decimal default(0),
l3 
decimal default(0),
l4 
decimal default(0),
l5 
decimal default(0),
l6 
decimal default(0),
l7 
decimal default(0),
l8 
decimal default(0),
l9 
decimal default(0),
l10 
decimal default(0),
l11 
decimal default(0),
l12 
decimal default(0)
)

insert into #aa
select td.fname,te.fname,tog.fname,
sum(case when po.fperiod=1 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=2 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=3 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=4 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=5 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=6 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=7 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=8 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=9 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=10 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=11 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=12 and year(po.fdate)=@year then po.famount else 0 end),
sum(case when po.fperiod=1 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=2 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=3 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=4 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=5 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=6 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=7 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=8 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=9 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=10 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=11 and year(po.fdate)=@year-1 then po.famount else 0 end),
sum(case when po.fperiod=12 and year(po.fdate)=@year-1 then po.famount else 0 end)
from t_rp_contact po,t_department td,t_emp te,t_organization tog
where po.ftype=5 and po.fcustomer=tog.fitemid
and te.fitemid=po.femployee and po.fdepartment=td.fitemid
group by td.fname,te.fname,tog.fname


insert into #aa
select fdept,femp+'合计','',sum(f1),sum(f2),sum(f3),sum(f4),sum(f5),sum(f6),sum(f7),sum(f8),sum(f9),sum(f10),sum(f11),sum(f12),
sum(l1),sum(l2),sum(l3),sum(l4),sum(l5),sum(l6),sum(l7),sum(l8),sum(l9),sum(l10),sum(l11),sum(l12)
from #aa
group by fdept,femp

insert into #aa
select fdept+'合计','','',sum(f1),sum(f2),sum(f3),sum(f4),sum(f5),sum(f6),sum(f7),sum(f8),sum(f9),sum(f10),sum(f11),sum(f12),
sum(l1),sum(l2),sum(l3),sum(l4),sum(l5),sum(l6),sum(l7),sum(l8),sum(l9),sum(l10),sum(l11),sum(l12)
from #aa
where femp like '%合计%'
group by fdept

select fdept as 部门,femp as 职员,fcust as 客户,f1 as 一月,f2 as 二月,f3 as 三月,f4 as 四月,f5 as 五月,f6 as 六月,
f7 
as 七月,f8 as 八月,f9 as 九月,f10 as 十月,f11 as 十一月,f12 as 十二月
,l1 
as 上年一月,l2 as 上年二月,l3 as 上年三月,l4 as 上年四月,l5 as 上年五月,l6 as 上年六月,
l7 
as 上年七月,l8 as 上年八月,l9 as 上年九月,l10 as 上年十月,l11 as 上年十一月,l12 as 上年十二月
from #aa
order by fdept,femp,fcust

drop table #aa

--select * from t_rp_contact

GO
posted @ 2008-08-07 02:21  arrowolf  阅读(436)  评论(0)    收藏  举报