这么长的存储区过程让我改,我都不知道每个表对应是干啥的,字段也没有说明。真的要崩溃了。。。

ALTER PROCEDURE  [dbo].[proc_yeyoo_sfdz]
@date1 varchar(40), 
@date2 varchar(40), 
@gbdm varchar(100)
as
select gbdm,sum(cz_jy_jbzc)cz_jy_jbzj,SUM(cz_jy_xmzj) cz_jy_xmzj,sum(cz_jy_jbzc)+SUM(cz_jy_xmzj) cz_jy_xj,
SUM(cz_wlzj_jy) cz_wlzj_jy,0 cz_wlzj_dwzck, 0 cz_wlzj_gzdk,SUM(cz_wlzj_jy) cz_wlzj_xj,
sum(cz_jy_jbzc)+SUM(cz_jy_xmzj)+SUM(cz_wlzj_jy) cz_zj,
SUM(cz_sjbz_by) cz_sjbz_by,SUM(cz_sjbz_lj) cz_sjbz_lj,SUM(cz_qtsr_by) cz_qtsr_by,SUM(cz_qtsr_lj) cz_qtsr_lj,SUM(cz_ysn_by) cz_ysn_by,SUM(cz_ysn_lj) cz_ysn_lj 
from (
--基本支出余额
select @gbdm gbdm,sum(ncye)+sum(fs) as cz_jy_jbzc,0 cz_jy_xmzj,0 cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
0 cz_qtsr_by,0 cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj from (
Select dm,isnull(ncye,0) as ncye,fs=0 From user99972017..bmhz where kmdm = '20301' and dm = @gbdm
union all
Select bmry as dm,ncye = 0,(sum(isnull(d,0))-sum(isnull(j,0))) as fs From user99972017..v_zw_pzhz where kmdm = '20301' and bmry = @gbdm  and pzrq<=@date2 group by bmry
) aa group by dm

union all
--项目支出余额
select @gbdm gbdm,0 cz_jy_jbzc,sum(ncye)+sum(fs) as cz_jy_xmzj,0 cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
0 cz_qtsr_by,0 cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj  from (
Select dm,isnull(ncye,0) as ncye,fs=0 From user99972017..bmhz where kmdm = '20302' and dm = @gbdm
union all
Select bmry as dm,ncye = 0,(sum(isnull(d,0))-sum(isnull(j,0))) as fs From user99972017..v_zw_pzhz where kmdm = '20302' and bmry = @gbdm and  pzrq<=@date2 group by bmry
) aa group by dm

union all
--往来结余余额
select @gbdm gbdm,0 cz_jy_jbzc,0 as cz_jy_xmzj,sum(ncye)+sum(fs) as cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
0 cz_qtsr_by,0 cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj  from (
Select dm,isnull(ncye,0) as ncye,fs=0 From user99972017..bmhz where kmdm = '20303' and dm = @gbdm
union all
Select bmry as dm,ncye = 0,(sum(isnull(d,0))-sum(isnull(j,0))) as fs From user99972017..v_zw_pzhz where kmdm = '20303' and bmry = @gbdm  and pzrq<=@date2 group by bmry
) aa group by dm

--select @gbdm gbdm,0 cz_jy_jbzc,0 cz_jy_xmzj,(sum(jhje)-sum(zfje))  as cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
--0 cz_qtsr_by,0 cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj from (
--select gbdm ,sum(je3) as jhje,zfje = 0 from z_qtykjh where def_str6 = 'over'  and def_dt1>=@date1 and def_dt1<=@date2
--and gbdm = @gbdm and def_Str2 in('0302','0307') group by gbdm
--union all
--select gbdm,jhje = 0,SUM(je) as zfje from z_qtzjzf where def_str3 = 'over' and shr4 is null  and rq >=@date1 and rq <=@date2
--and yslx in('0302','0307') and gbdm = @gbdm group by gbdm ) aa group by gbdm

union all
--上级补助收入
select @gbdm gbdm,0 cz_jy_jbzc,0 cz_jy_xmzj,0 cz_wlzj_jy,
SUM(case when rq >=@date1 and rq <=@date2 then je end) as cz_sjbz_by,SUM(case when rq <=@date2 then je end) as cz_sjbz_lj,0 cz_qtsr_by,0 cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj 
from z_qtzjzf where def_str3 = 'over' and shr4 is null and yslx = '0303' and gbdm = @gbdm group by yslx

union all
--其他收入
select @gbdm gkxm,0 cz_jy_jbzc,0 cz_jy_xmzj,0 cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
SUM(case when rq >=@date1 and rq <=@date2 then je end) as cz_qtsr_by,SUM(case when rq <=@date2 then je end) as cz_qtsr_lj,0 cz_ysn_by,0 cz_ysn_lj 
from z_qtzjzf where def_str3 = 'over' and shr4 is null and yslx = '0309' and gbdm = @gbdm group by yslx

union all
--预算内
--select @gbdm gbdm,0 cz_jy_jbzc,0 cz_jy_xmzj,0 cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,0 cz_qtsr_by, 
--0 cz_qtsr_lj,SUM(case when rq >=@date1 and rq <=@date2 then je end) as cz_ysn_by,SUM(case when rq <=@date2 then je end) as cz_ysn_lj 
--from z_qtzjzf where def_str3 = 'over' and shr4 is null  and yslx in('0101','0102','0201','08') and gbdm =@gbdm group by yslx


select @gbdm gbdm,0 cz_jy_jbzc,0 as cz_jy_xmzj,0 cz_wlzj_jy,0 cz_sjbz_by,0 cz_sjbz_lj,
0 cz_qtsr_by,0 cz_qtsr_lj,SUM(by0) cz_ysn_by,sum(fs) as cz_ysn_lj  from (
Select bmry as dm,(sum(isnull(j,0))-sum(isnull(d,0))) as fs,sum(case when pzrq >= @date1 and pzrq<=@date2 then isnull(j,0)-isnull(d,0) end) by0
From user99992017..v_zw_pzhz where  bmry = @gbdm and pzrq<=@date2 group by bmry
) aa group by dm

)a group by gbdm

————————————————分割线————————————————————————————

哈哈,问题解决了,原来字段都是存在的,只要改最上面的项目头,把字段设置成为自己需要的!

猜你喜欢

转载自blog.csdn.net/SicongFu/article/details/76682644
今日推荐