日期:2014-05-17 浏览次数:20487 次
create table #rourou
(
f_spbm varchar(50) not null,
f_qcsl float,
)
create table #rourou2
(
f_spbm varchar(50) not null,
f_chl float,
)
create table #rourou3
(
f_spbm varchar(50) not null,
f_kcsl float,
f_pjkcsl float
)
create table #rourou4
(
f_spbm varchar(50) not null,
f_chl float,
f_pjchl float
)
declare @starttime datetime
declare @endtime datetime
declare @startny varchar(10)
declare @ts int
declare @zts int
declare @startday varchar(10)
declare @null varchar(10)
set @starttime='[开始日期]'
set @endtime='[结束日期]'
set @startny=(select left(convert(varchar(10),@starttime,112),6))
set @ts=(select datediff(day,@starttime,@endtime))+1
set @startday=(select convert(varchar(10),@starttime,112))
set @zts=@ts
insert into #rourou exec('select a.f_spbm,'+@zts+'*(isnull(c.f_qcsl,0)+isnull(d.f_rhj,0)) f_qmsl from tbspda a
left join tb'+@startny+'_yhj c on a.f_spbm=c.f_spbm
left join (select f_spbm,isnull(sum(f_jhsl+f_brsl+f_sysl+f_pssl-f_xssl-f_bcsl-f_shsl-f_phsl),0) f_rhj from tb'+@startny+'_rhj where f_rq<'+@startday+' group by f_spbm) d on a.f_spbm=d.f_spbm where datalength(a.f_cwbm)<>0')
while @ts>'0'
begin
declare @jsny varchar(10)
declare @jsday varchar(10)
set @jsny=(select left(convert(varchar(10),@starttime,112),6))
set @jsday=(select convert(varchar(10),@starttime,112))
insert into #rourou exec('select f_spbm,'+@ts+'*isnull(b.f_Jhsl+b.f_Brsl+b.f_Sysl+b.f_Pssl-b.f_Xssl-b.f_Bcsl-b.f_Shsl-b.f_Phsl,0) from tb'+@jsny+'_rhj b where b.f_rq='+@jsday)
set @starttime=@starttime+1
set @ts=@ts-1
insert into #rourou2 exec('select f_spbm,isnull(f_xssl+f_bcsl+f_shsl+f_phsl,0) f_chl from tb'+@jsny+'_rhj where f_rq='+@jsday)
end
insert into #rourou4 exec('select f_spbm,sum(f_chl),sum(f_chl)/'+@zts+' from #rourou2 group by f_spbm')
insert into #rourou3 exec('select f_spbm,sum(f_qcsl) f_kcsl,sum(f_qcsl)/'+@zts+' from #rourou group by f_spbm')
select a.f_spbm,c.f_sptm,b.f_spmc,a.f_pjkcsl f_pjkcsl,isnull(a.f_pjkcsl/e.f_dwbl,0) f_pjkcxs,case when (b.f_ti*b.f_hi)<>'0' then isnull(a.f_pjkcsl/e.f_dwbl/(b.f_ti*b.f_hi),0) else '0' end f_pjkctp,isnull(f.f_pjchl,0) f_pjchl,case when (b.f_ti*b.f_hi)<>'0' then isnull(f.f_pjchl/e.f_dwbl/(b.f_ti*b.f_hi),0) else '0' end f_pjchtp,isnull(f.f_chl,0) f_chl,case when (b.f_ti*b.f_hi)<>'0' then isnull(f.f_chl/e.f_dwbl/(b.f_ti*b.f_hi),0) else '0' end f_chtp,case when a.f_kcsl<>'0' then&nb