金蝶K3订单发货汇款情况表
统计部门业务员的订单、通知单、发票、收款的本月、上月、上季度、上年对比情况
create proc stkd_销售_订单发货汇款情况表 --stkd_销售_订单发货汇款情况表 '2006-1-1','2006-6-30'
@begdate datetime,
@enddate datetime
as
set nocount on
declare @monthbeg datetime,@lastmonthb datetime,@lastyearb datetime,@lastmonthe datetime,@lastyeare datetime
set @monthbeg=cast((cast(year(@begdate) as varchar(4))+'-'+cast(month(@begdate) as varchar(2))+'-01') as datetime)
set @lastmonthb=dateadd(month,-1,@monthbeg)
set @lastmonthe=dateadd(month,-1,@enddate)
set @lastyearb=dateadd(year,-1,@begdate)
set @lastyeare=dateadd(year,-1,@enddate)
/*
declare @a datetime,@b datetime
select @a ='2006-9-1'
select @b=dateadd(month,-1,@a)
select @b
*/
create table #aa(
fitemid int,
fdept varchar(80),
fname varchar(20),
forweek decimal(18,2) default(0),
formonth decimal(18,2) default(0),
fornotfinish decimal(18,2) default(0),
forlastmonth decimal(18,2) default(0),
forlastyear decimal(18,2) default(0),
fseweek decimal(18,2) default(0),
fsemonth decimal(18,2) default(0),
fselastmonth decimal(18,2) default(0),
fselastyear decimal(18,2) default(0),
finweek decimal(18,2) default(0),
finmonth decimal(18,2) default(0),
finlastmonth decimal(18,2) default(0),
finlastyear decimal(18,2) default(0)
)

insert into #aa(fitemid,fname,fdept)
select te.fitemid,te.fname,td.fname
from t_emp te left join t_department td
on te.fdepartmentid=td.fitemid

update t1
set t1.forweek=t2.fweek,t1.formonth=t2.fmonth,t1.forlastmonth=t2.flastmonth,t1.forlastyear=t2.flastyear
from #aa t1,
(select se.fempid,sum(case when se.fdate>=@begdate and se.fdate<=@enddate then see.fqty else 0 end) as fweek,
sum(case when se.fdate>=@monthbeg and se.fdate<=@enddate then see.fqty else 0 end) as fmonth,
sum(case when se.fdate>=@lastyearb and se.fdate<=@lastyeare then see.fqty else 0 end) as flastyear,
sum(case when se.fdate>=@lastmonthb and se.fdate<=@lastmonthe then see.fqty else 0 end) as flastmonth
from seorder se,seorderentry see
where se.finterid=see.finterid
group by se.fempid
) t2
where t1.fitemid=t2.fempid
update t1
set t1.fseweek=t2.fweek,t1.fsemonth=t2.fmonth,t1.fselastmonth=t2.flastmonth,t1.fselastyear=t2.flastyear
from #aa t1,
(select se.fempid,sum(case when se.fdate>=@begdate and se.fdate<=@enddate then see.fqty else 0 end) as fweek,
sum(case when se.fdate>=@monthbeg and se.fdate<=@enddate then see.fqty else 0 end) as fmonth,
sum(case when se.fdate>=@lastyearb and se.fdate<=@lastyeare then see.famount else 0 end) as flastyear,
sum(case when se.fdate>=@lastmonthb and se.fdate<=@lastmonthe then see.fqty else 0 end) as flastmonth
from SEOutStock se,SEOutStockentry see
where se.finterid=see.finterid
group by se.fempid
) t2
where t1.fitemid=t2.fempid
update t1
set t1.finweek=t2.fweek,t1.finmonth=t2.fmonth,t1.finlastmonth=t2.flastmonth,t1.finlastyear=t2.flastyear
from #aa t1,
(select se.femployee,sum(case when se.fdate>=@begdate and se.fdate<=@enddate then se.famount else 0 end) as fweek,
sum(case when se.fdate>=@monthbeg and se.fdate<=@enddate then se.famount else 0 end) as fmonth,
sum(case when se.fdate>=@lastyearb and se.fdate<=@lastyeare then se.famount else 0 end) as flastyear,
sum(case when se.fdate>=@lastmonthb and se.fdate<=@lastmonthe then se.famount else 0 end) as flastmonth
from t_rp_contact se
where se.ftype=5
group by se.femployee
) t2
where t1.fitemid=t2.femployee
delete from #aa where forweek=0 and formonth=0 and finweek=0 and finmonth=0 and fseweek=0 and fsemonth=0
insert into #aa(fdept,forweek,formonth,fornotfinish,forlastmonth,forlastyear,fseweek,fsemonth,fselastmonth,
fselastyear,finweek,finmonth,finlastmonth,finlastyear)
select fdept+'合计',sum(forweek),sum(formonth),sum(fornotfinish),sum(forlastmonth),sum(forlastyear),sum(fseweek),
sum(fsemonth),sum(fselastmonth),
sum(fselastyear),sum(finweek),sum(finmonth),sum(finlastmonth),sum(finlastyear)
from #aa
group by fdept
select fdept as 部门,fname as 业务员,forweek as 订单本周,formonth as 订单本月,fornotfinish as 未完成订单,forlastmonth as 订单上月,
forlastyear as 订单上年,fseweek as 通知单本周,fsemonth as 通知单本月,fselastmonth as 通知单上月,fselastyear as 通知单上年,
finweek as 收款本周,finmonth as 收款本月,finlastmonth as 收款上月,finlastyear as 收款上年
from #aa
order by fdept,fname
drop table #aa

GO


浙公网安备 33010602011771号