IFEXISTS (SELECT*FROM sys.objects WHEREobject_id=OBJECT_ID(N'[dbo].[getbetweenDays]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROPFUNCTION[dbo].[getbetweenDays] go IFEXISTS (SELECT*FROM sys.objects WHEREobject_id=OBJECT_ID(N'[dbo].[getDay]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROPFUNCTION[dbo].[getDay] go IFEXISTS (SELECT*FROM sys.objects WHEREobject_id=OBJECT_ID(N'[dbo].[getDays]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROPFUNCTION[dbo].[getDays] go IFEXISTS (SELECT*FROM sys.objects WHEREobject_id=OBJECT_ID(N'[dbo].[hjtj]') AND type in (N'P', N'PC')) DROPPROCEDURE[dbo].[hjtj] go IFEXISTS (SELECT*FROM sys.objects WHEREobject_id=OBJECT_ID(N'[dbo].[hjtjold]') AND type in (N'P', N'PC')) DROPPROCEDURE[dbo].[hjtjold] go SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: 郑平 -- Create date: 2008年7月9日9:30:49 -- Description: 比较两个时间段间交叉的天数 -- 判断(begin1,end1)时间段在(@begin2,@end2)中有几天(包含当天) -- 调用[dbo].[getDays]('2008-06-06','2008-07-06','2008-07-01','2008-07-09') -- 意思是:2008-06-06到2008-07-06中在'2008-07-01'到'2008-07-09'有几天 -- ============================================= createfunction[dbo].[getbetweenDays] (@begin1varchar(12),@end1varchar(12),@begin2varchar(12),@end2varchar(12),@begin3varchar(12),@end3varchar(12)) returnsint as begin declare@numasint; declare@beginDateasdatetime; declare@endDateasdatetime; declare@beginDate1asdatetime; declare@endDate1asdatetime; declare@beginDate2asdatetime; declare@endDate2asdatetime; set@beginDate=cast(@begin1asdatetime); set@endDate=cast(@end1asdatetime); set@beginDate1=cast(@begin2asdatetime); set@endDate1=cast(@end2asdatetime); set@beginDate2=cast(@begin3asdatetime); set@endDate2=cast(@end3asdatetime); set@num=0; --两个时间交集 if(@begindate2<=@begindate1and@enddate2<=@endDate1) set@num=datediff(day,@begindate,@enddate)+1; return@num; end go -- ============================================= -- Author: 郑平 -- Create date: 2008年7月9日9:30:49 -- Description: 比较两个字符串间的天数 -- 判断(@begin时间在(@end)中有几天(包含当天) -- 调用[dbo].[getDays]('2008-06-06','2008-07-06') -- 意思是:2008-06-06到2008-07-06有几天 -- ============================================= CREATEfunction[dbo].[getDay]( @beginvarchar(12), @endvarchar(12) )returnsint begin declare@numasint set@num=0; declare@beginDateasdatetime; declare@endDateasdatetime; if(@beginisnulland@endisnull) return100000000; if(@begin>@end) return100000000; set@beginDate=cast(@beginasdatetime) set@endDate=cast(@endasdatetime) set@num=datediff(day,@begindate,@enddate)+1; return@num; end GO -- ============================================= -- Author: 郑平 -- Create date: 2008年7月9日9:30:49 -- Description: 比较两个时间段间交叉的天数 -- 判断(begin1,end1)时间段在(@begin2,@end2)中有几天(包含当天) -- 调用[dbo].[getDays]('2008-06-06','2008-07-06','2008-07-01','2008-07-09') -- 意思是:2008-06-06到2008-07-06中在'2008-07-01'到'2008-07-09'有几天 -- ============================================= CREATEfunction[dbo].[getDays] (@begin1varchar(12),@end1varchar(12),@begin2varchar(12),@end2varchar(12)) returnsint as begin declare@numasint; declare@beginDateasdatetime; declare@endDateasdatetime; declare@beginDate1asdatetime; declare@endDate1asdatetime; set@beginDate=cast(@begin1asdatetime); set@endDate=cast(@end1asdatetime); set@beginDate1=cast(@begin2asdatetime); set@endDate1=cast(@end2asdatetime); set@num=0; --如果两个时间差没有交集 if((@begindate<@begindate1and@enddate<@begindate1) or (@begindate>@enddate1)) begin return@num; end; --结束时间大于开始时间 if(@enddate>=@begindate1and@begindate<=@begindate1) set@num=datediff(day,@begindate1,@enddate)+1; --时间在另个时间的内e if(@beginDate>=@begindate1and@enddate<=@enddate1) set@num=datediff(day,@begindate,@enddate)+1; --开始时间大于开始时间,结束时间大于结束时间 if(@begindate>@begindate1and@enddate>@enddate1and@begindate<=@enddate1) set@num=datediff(day,@begindate,@enddate1)+1; if(@begindate<=@begindate1and@enddate>=@enddate1and@begindate1<=@enddate) set@num=datediff(day,@begindate1,@enddate1)+1; return@num; end Go GO CREATEproc[dbo].[hjtj]( @orgidvarchar(32), @begindatevarchar(12), @enddatevarchar(12) ) as set nocount on; --办事处临时表 createtable #OrgOffice( id char(32), ) --主计划临时表 createtable #mainplan( id char(32) ) --类型临时表 createtable #type( id char(32) ); createtable #tb( typename varchar(30), totalfee numeric(18,3), months varchar(32), totalamount numeric(20,3) ) --取出该类型的全部子类型(包括自己) /**//* insert into #type(id,pid,typeName) select id,pid,'终端类型' from selectitem where pid='402881e80caa9192010caa971be5000c' union all union all select id,pid,'非终端类型' from selectitem a where a.pid='402881e80caa9192010caa97560f000e' union all select id,pid,'地面广告宣传 ' from selectitem a where a.pid='402881e80caa9192010caa9806450010' union all select id,pid,'周边开发 ' from selectitem a where a.pid='402881e80caa9192010caa9989c80015' union all select id,id,'总部宣传品 ' from selectitem a where a.id='402881e80caa9192010caa9868b80012' */ insertinto #type(id) select id from selectitem a where a.pid='402881e80caa9192010caa971be5000c' or a.typeid='402881e80caa9192010caa96a85a000a' or a.pid='402881e80caa9192010caa97560f000e' or a.pid='402881e80caa9192010caa9806450010' or a.pid='402881e80caa9192010caa9989c80015' or a.id='402881e80caa9192010caa9868b80012' --递归查询全部下属机构 ; with org(orgid) as ( select a.oid from orgunitlink a where a.pid=@orgid unionall select c.oid from orgunitlink c innerjoin org aa on aa.orgid=c.pid ) insertinto #OrgOffice(id) select a.orgid from org a unionselect@orgid --插入机构下的主计划 insertinto #mainplan select a.id from Mfm_Mainplaninfo a innerjoin #orgoffice b on b.id=a.orgunitid declare@beginmonthasdatetime; declare@endmonthasdatetime; declare@beginmonth1asvarchar(10) declare@endmonth1asvarchar(10) set@beginmonth=cast(@begindateasdatetime); set@endmonth=cast(@enddateasdatetime); while@beginmonth<=@endmonth begin set@beginmonth1=convert(varchar(10),@beginmonth,120) set@endmonth1=convert(varchar(10),@endmonth,120) if(dateadd(day,-1,cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime))>=@enddate)--结束时间在小于当月最后一天 begin --终端促销日计划金额,日任务量 insertinto #tb(typename,totalfee,months,totalamount) select'终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_terminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 andnotexists (select'X'from delobj delobj1_ where Mfm_terminalplan.id=delobj1_.objid) unionall --非终端日计划金额,日任务量 select'非终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Noterminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --地面广告宣传日计划金额,日任务量 select'地面宣传活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Groundplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --周边开发 select'周边开发活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Areadevplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --总部宣传品 select'总部宣传品活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Groupmaterialplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 --媒体类型 unionall select at.objname, cast(sum(isnull(a1.feeamount,0)/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*a1.inout/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from mfm_mediadetailplaninfo a1 innerjoin mfm_mediaplaninfo mm on mm.id = a1.mediaplanid crossjoin selectitem at where a1.mediaplanid in( selectdistinct m.id from mfm_mediaplaninfo m innerjoin mfm_mediadetailplaninfo md on m.id = md.mediaplanid innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a'and a.id=m.mediatypeid innerjoin #mainplan c on m.mainplanid =c.id where md.begindate <=@endmonth1and md.enddate >=@beginmonth1 and md.begindate<=md.enddate and md.mark=1) and a1.mark=1 and at.id=mm.mediatypeid and at.typeid='402881e80caa9192010caa96a85a000a' groupby at.objname insertinto #tb(typename,totalfee,months) select a.objname,0,convert(varchar(7),@beginmonth,120) from selectitem a where a.typeid='402881e80caa9192010caa96a85a000a'and notexists (select1from #tb t where t.typename=a.objname and t.months=convert(varchar(7),@beginmonth,120)) set@beginmonth=dateadd(month,3,@endmonth);--设置时间大于结束时间,循环结束 end else--结束时间在大于当月最后一天 begin --set @beginmonth=@begindate; set@endmonth=dateadd(day,-1,cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime));--本月的最后一天 if(@endmonth>=@enddate) set@endmonth=@enddate;--假如下一个月最后一天大于结束时间 set@beginmonth1=convert(varchar(10),@beginmonth,120) set@endmonth1=convert(varchar(10),@endmonth,120) --运行计算 insertinto #tb(typename,totalfee,months,totalamount) select'终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_terminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 andnotexists (select'X'from delobj delobj1_ where Mfm_terminalplan.id=delobj1_.objid) unionall --非终端日计划金额,日任务量 select'非终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Noterminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --地面广告宣传日计划金额,日任务量 select'地面宣传活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Groundplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --周边开发 select'周边开发活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Areadevplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 unionall --总部宣传品 select'总部宣传品活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from Mfm_Groupmaterialplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=1and planstatus=2 --媒体类型 unionall select at.objname, cast(sum(isnull(a1.feeamount,0)/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*a1.inout/dbo.getday(begindate,enddate)*dbo.getdays(@beginmonth1,@endmonth1,begindate,enddate)) as numeric(22,3)) from mfm_mediadetailplaninfo a1 innerjoin mfm_mediaplaninfo mm on mm.id = a1.mediaplanid crossjoin selectitem at where a1.mediaplanid in( selectdistinct m.id from mfm_mediaplaninfo m innerjoin mfm_mediadetailplaninfo md on m.id = md.mediaplanid innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a'and a.id=m.mediatypeid innerjoin #mainplan c on m.mainplanid =c.id where md.begindate <=@endmonth1and md.enddate >=@beginmonth1 and md.begindate<=md.enddate and md.mark=1) and a1.mark=1 and at.id=mm.mediatypeid and at.typeid='402881e80caa9192010caa96a85a000a' groupby at.objname --select @beginmonth--2008-05-01 00:00:00.000 --select @endmonth insertinto #tb(typename,totalfee,months) select a.objname,0,convert(varchar(7),@beginmonth,120) from selectitem a where a.typeid='402881e80caa9192010caa96a85a000a'and notexists (select1from #tb t where t.typename=a.objname and t.months=convert(varchar(7),@beginmonth,120)) --计算完毕后, set@beginmonth=cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime);--开始时间为下个月的第一天 set@endmonth=@enddate end end --总投入 insertinto #tb(typename,totalfee,months) select typename,sum(totalfee),'总投入' from #tb groupby typename --计算任务调整 insertinto #tb(typename,totalfee,months) select a.objname,sum(field008),'任务调整'from ufg0z3a51168936462406 innerjoin selectitem a on a.id=field005 where field011='4028827015f58e7901161d0b63236364' and field012='40288141103870fb0110487902250403' and field005 in ('402881e80caa9192010caa971be5000c'--终端类型 , '402881e80caa9192010caa97560f000e'--非终端类型 ,'402881e80caa9192010caa9806450010'--地面广告宣传 ,'402881e80caa9192010caa9989c80015'--周边开发 ,'402881e80caa9192010caa9868b80012'--总部宣传品 ) and field018='1' and field002 in (select id from #orgoffice) groupby a.objname unionall select a.objname,sum(field008),'任务调整'from ufg0z3a51168936462406 innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a' and a.id=field025 where field011='4028827015f58e7901161d0b63236364' and field012='40288141103870fb0110487902250402' and field018='1' and field002 in (select id from #orgoffice ) groupby a.objname--通过媒体类型聚合 --预留额度(机动费) insertinto #tb(typename,totalfee,months) select ks.objname,sum(a.feeamount*cast(a.col1 as numeric(10,2))),'预留机动费'from Mfm_Budget a innerjoin #mainplan b on b.id=a.mainplanid innerjoin (selecttop1*from Mfm_Assessinfo where dbo.getdays(@begindate,@enddate,begindate,enddate)>0) c on assessinfoid=c.id innerjoin selectitem k on k.id=a.acttypeid and (k.pid='402881e80caa9192010caa97560f000e'or k.pid='402881e80caa9192010caa971be5000c' or k.pid='402881e80caa9192010caa9806450010' or k.pid='402881e80caa9192010caa9989c80015') innerjoin selectitem ks on ks.id=k.pid and isactive='1' groupby ks.objname unionall--电视媒体,总部宣传品 select k.objname,sum(a.feeamount*cast(a.col1 as numeric(10,2))),'预留机动费'from Mfm_Budget a innerjoin #mainplan b on b.id=a.mainplanid innerjoin (selecttop1*from Mfm_Assessinfo where dbo.getdays(@begindate,@enddate,begindate,enddate)>0) c on assessinfoid=c.id innerjoin selectitem k on k.id=a.acttypeid and (k.typeid='402881e80caa9192010caa96a85a000a' or k.id='402881e80caa9192010caa9868b80012') and isactive='1' groupby k.objname --插入没有数据的机动费,默认设置为0 insertinto #tb(typename,totalfee,months) select a.objname,0,'预留机动费'from selectitem a where a.id='402881e80caa9192010caa971be5000c' or a.typeid='402881e80caa9192010caa96a85a000a' or a.id='402881e80caa9192010caa97560f000e' or a.id='402881e80caa9192010caa9806450010' or a.id='402881e80caa9192010caa9989c80015' or a.id='402881e80caa9192010caa9868b80012' andnotexists(select*from #tb c where c.months='预留机动费'and a.objname=c.typename) --插入总投入 insertinto #tb(typename,totalfee,months) select typename,sum(totalamount),'总任务额度' from #tb where totalamount>0 groupby typename --加上预留机动费 update #tb set totalfee=totalfee+c from #tb innerjoin (select a.typename, sum(totalfee) c from #tb a where a.months='预留机动费' groupby a.typename) c on c.typename=#tb.typename where #tb.months='总任务额度' --加上任务调整 update #tb set totalfee=totalfee+c from #tb innerjoin (select a.typename, sum(totalfee) c from #tb a where a.months='任务调整' groupby a.typename) c on c.typename=#tb.typename where #tb.months='总任务额度' declare@sqlasvarchar(4000); set@sql='select typename [活动类型]' select@sql=@sql+',isnull(max(case when months='''+months+''' then totalfee end),0) ['+months+']'from (selectdistinct months from #tb ) b set@sql=@sql+' from #tb t group by typename order by reverse(typename) desc'; print@sql exec(@sql) go /**//****** 对象: StoredProcedure [dbo].[hjtjOrigin1] 脚本日期: 07/09/2008 13:54:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO createproc[dbo].[hjtjold]( @orgidvarchar(32), @begindatevarchar(12), @enddatevarchar(12) ) as set nocount on; --办事处临时表 createtable #OrgOffice( id char(32), company char(32) ) --主计划临时表 createtable #mainplan( id char(32) ) --类型临时表 createtable #type( id char(32) --pid varchar(32), --typeName varchar(32) --上级类型的名称,如(非终端,周边开发) ); createtable #tb( typename varchar(30), totalfee numeric(18,3), months varchar(32), totalamount numeric(20,3) ) declare@regionSasvarchar(500); --片区 set@regions='402881ba0d6777c5010d68a41ee70037,402881ba0d6777c5010d68a7432f003b,402881ba0d6777c5010d68ab52c30047,402881ba0d6777c5010d68abcc94004a,402881ba0d6777c5010d68ac318b004d' --取出该类型的全部子类型(包括自己) /**//* insert into #type(id,pid,typeName) select id,pid,'终端类型' from selectitem where pid='402881e80caa9192010caa971be5000c' union all union all select id,pid,'非终端类型' from selectitem a where a.pid='402881e80caa9192010caa97560f000e' union all select id,pid,'地面广告宣传 ' from selectitem a where a.pid='402881e80caa9192010caa9806450010' union all select id,pid,'周边开发 ' from selectitem a where a.pid='402881e80caa9192010caa9989c80015' union all select id,id,'总部宣传品 ' from selectitem a where a.id='402881e80caa9192010caa9868b80012' */ insertinto #type(id) select id from selectitem a where a.pid='402881e80caa9192010caa971be5000c' or a.typeid='402881e80caa9192010caa96a85a000a' or a.pid='402881e80caa9192010caa97560f000e' or a.pid='402881e80caa9192010caa9806450010' or a.pid='402881e80caa9192010caa9989c80015' or a.id='402881e80caa9192010caa9868b80012' --递归查询全部下属机构 ; with org(orgid) as ( select a.oid from orgunitlink a where a.pid=@orgid unionall select c.oid from orgunitlink c innerjoin org aa on aa.orgid=c.pid ) insertinto #OrgOffice(id) select a.orgid from org a --插入机构下的主计划 insertinto #mainplan select a.id from Mfm_Mainplaninfo a innerjoin #orgoffice b on b.id=a.orgunitid declare@beginmonthasdatetime; declare@endmonthasdatetime; declare@beginmonth1asvarchar(10) declare@endmonth1asvarchar(10) set@beginmonth=cast(@begindateasdatetime); set@endmonth=cast(@enddateasdatetime); while@beginmonth<=@endmonth begin set@beginmonth1=convert(varchar(10),@beginmonth,120) set@endmonth1=convert(varchar(10),@endmonth,120) if(dateadd(day,-1,cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime))>=@enddate)--结束时间在小于当月最后一天 begin --终端促销日计划金额,日任务量 insertinto #tb(typename,totalfee,months,totalamount) select'终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_terminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --非终端日计划金额,日任务量 select'非终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Noterminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --地面广告宣传日计划金额,日任务量 select'地面宣传活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Groundplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --周边开发 select'周边开发活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Areadevplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --总部宣传品 select'总部宣传品活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Groupmaterialplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 --媒体类型 unionall select at.objname, cast(sum(isnull(a1.feeamount,0)/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*a1.inout/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from mfm_mediadetailplaninfo a1 innerjoin mfm_mediaplaninfo mm on mm.id = a1.mediaplanid crossjoin selectitem at where a1.mediaplanid in( selectdistinct m.id from mfm_mediaplaninfo m innerjoin mfm_mediadetailplaninfo md on m.id = md.mediaplanid innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a'and a.id=m.mediatypeid innerjoin #mainplan c on m.mainplanid =c.id where md.begindate <=@endmonth1and md.enddate >=@beginmonth1 and md.begindate<=md.enddate and md.mark=0) and a1.mark=0 and at.id=mm.mediatypeid and at.typeid='402881e80caa9192010caa96a85a000a' groupby at.objname --111 select * from #tb --插入没有该媒体类型(电视,网络等)数据为0 insertinto #tb(typename,totalfee,months) select a.objname,0,convert(varchar(7),@beginmonth,120) from selectitem a where a.typeid='402881e80caa9192010caa96a85a000a'and notexists (select1from #tb t where t.typename=a.objname and t.months=convert(varchar(7),@beginmonth,120)) set@beginmonth=dateadd(month,3,@endmonth);--设置时间大于结束时间,循环结束 end else--结束时间在大于当月最后一天 begin set@endmonth=dateadd(day,-1,cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime));--本月的最后一天 if(@endmonth>=@enddate) set@endmonth=@enddate;--假如下一个月最后一天大于结束时间 set@beginmonth1=convert(varchar(10),@beginmonth,120) set@endmonth1=convert(varchar(10),@endmonth,120) --运行计算 insertinto #tb(typename,totalfee,months,totalamount) select'终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_terminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --非终端日计划金额,日任务量 select'非终端促销活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Noterminalplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --地面广告宣传日计划金额,日任务量 select'地面宣传活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Groundplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --周边开发 select'周边开发活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Areadevplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 unionall --总部宣传品 select'总部宣传品活动', cast(sum(feeamount/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*inoutratio/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from Mfm_Groupmaterialplan innerjoin Mfm_Mainplaninfo a on mainplanid=a.id innerjoin #orgoffice b on b.id=a.orgunitid innerjoin #type on acttype=#type.id innerjoin orgunit on orgunit.id=b.id innerjoin selectitem k on k.id=#type.id where begindate<=@endmonth1 and enddate>=@beginmonth1 and begindate<=enddate and mark=0 --媒体类型 unionall select at.objname, cast(sum(isnull(a1.feeamount,0)/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) 总投入 ,convert(varchar(7),@beginmonth,120) ,cast(sum(feeamount*a1.inout/dbo.getday(begindate,enddate)*dbo.getbetweenDays(@beginmonth1,@endmonth1,@begindate,@enddate,begindate,enddate)) as numeric(22,3)) from mfm_mediadetailplaninfo a1 innerjoin mfm_mediaplaninfo mm on mm.id = a1.mediaplanid crossjoin selectitem at where a1.mediaplanid in( selectdistinct m.id from mfm_mediaplaninfo m innerjoin mfm_mediadetailplaninfo md on m.id = md.mediaplanid innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a'and a.id=m.mediatypeid innerjoin #mainplan c on m.mainplanid =c.id where md.begindate <=@endmonth1and md.enddate >=@beginmonth1 and md.begindate<=md.enddate and md.mark=0) and a1.mark=0 and at.id=mm.mediatypeid and at.typeid='402881e80caa9192010caa96a85a000a' groupby at.objname --select * from #tb insertinto #tb(typename,totalfee,months) select a.objname,0,convert(varchar(7),@beginmonth,120) from selectitem a where a.typeid='402881e80caa9192010caa96a85a000a'and notexists (select1from #tb t where t.typename=a.objname and t.months=convert(varchar(7),@beginmonth,120)) --计算完毕后,开始时间为下个月的第一天 set@beginmonth=cast(convert(varchar(7),dateadd(month,1,@beginmonth),120)+'-01'asdatetime); set@endmonth=@enddate--结束时间为默认结束时间 end end --计算总投入 insertinto #tb(typename,totalfee,months) select typename,sum(totalfee),'总投入' from #tb groupby typename --任务调整 insertinto #tb(typename,totalfee,months) select a.objname,sum(field008),'任务调整'from ufg0z3a51168936462406 innerjoin selectitem a on a.id=field005 where field011='4028827015f58e7901161d0b63236364' and field012='40288141103870fb0110487902250403' and field005 in ('402881e80caa9192010caa971be5000c'--终端类型 , '402881e80caa9192010caa97560f000e'--非终端类型 ,'402881e80caa9192010caa9806450010'--地面广告宣传 ,'402881e80caa9192010caa9989c80015'--周边开发 ,'402881e80caa9192010caa9868b80012'--总部宣传品 ) and field018='1' and field002 in (select id from #orgoffice) and field027 isnull--为null为原始计划 groupby a.objname unionall select a.objname,sum(field008),'任务调整'from ufg0z3a51168936462406 innerjoin selectitem a on a.typeid='402881e80caa9192010caa96a85a000a' and a.id=field025 where field011='4028827015f58e7901161d0b63236364' and field012='40288141103870fb0110487902250402' and field018='1' and field002 in (select id from #orgoffice ) and field027 isnull--为null为原始计划 groupby a.objname--通过媒体类型聚合 --预留额度(机动费) insertinto #tb(typename,totalfee,months) select ks.objname,sum(a.feeamount*cast(a.col1 as numeric(10,2))),'预留机动费'from Mfm_Budget a innerjoin #mainplan b on b.id=a.mainplanid innerjoin (selecttop1*from Mfm_Assessinfo where dbo.getdays(@begindate,@enddate,begindate,enddate)>0) c on assessinfoid=c.id innerjoin selectitem k on k.id=a.acttypeid and (k.pid='402881e80caa9192010caa97560f000e'or k.pid='402881e80caa9192010caa971be5000c' or k.pid='402881e80caa9192010caa9806450010' or k.pid='402881e80caa9192010caa9989c80015') innerjoin selectitem ks on ks.id=k.pid and isactive='0' groupby ks.objname unionall--电视媒体,总部宣传品 select k.objname,sum(a.feeamount*cast(a.col1 as numeric(10,2))),'预留机动费'from Mfm_Budget a innerjoin #mainplan b on b.id=a.mainplanid innerjoin (selecttop1*from Mfm_Assessinfo where dbo.getdays(@begindate,@enddate,begindate,enddate)>0) c on assessinfoid=c.id innerjoin selectitem k on k.id=a.acttypeid and (k.typeid='402881e80caa9192010caa96a85a000a' or k.id='402881e80caa9192010caa9868b80012') and isactive='0' groupby k.objname --插入没有数据的机动费,默认设置为0 insertinto #tb(typename,totalfee,months) select a.objname,0,'预留机动费'from selectitem a where a.id='402881e80caa9192010caa971be5000c' or a.typeid='402881e80caa9192010caa96a85a000a' or a.id='402881e80caa9192010caa97560f000e' or a.id='402881e80caa9192010caa9806450010' or a.id='402881e80caa9192010caa9989c80015' or a.id='402881e80caa9192010caa9868b80012' andnotexists(select*from #tb c where c.months='预留机动费'and a.objname=c.typename) --计算总任务额度(预留机动费+任务调整+总投入) --插入总投入 insertinto #tb(typename,totalfee,months) select typename,sum(totalamount),'总任务额度' from #tb where totalamount>0 groupby typename --加上预留机动费 update #tb set totalfee=totalfee+c from #tb innerjoin (select a.typename, sum(totalfee) c from #tb a where a.months='预留机动费' groupby a.typename) c on c.typename=#tb.typename where #tb.months='总任务额度' --加上任务调整 update #tb set totalfee=totalfee+c from #tb innerjoin (select a.typename, sum(totalfee) c from #tb a where a.months='任务调整' groupby a.typename) c on c.typename=#tb.typename where #tb.months='总任务额度' --输出数据 declare@sqlasvarchar(4000); set@sql='select typename [活动类型]' select@sql=@sql+',isnull(max(case when months='''+months+''' then totalfee end),0) ['+months+']'from (selectdistinct months from #tb ) b set@sql=@sql+' from #tb t group by typename order by reverse(typename) desc'; print@sql exec(@sql) go