USE [ibcs_baotou_true]
GO
/****** Object:  StoredProcedure [dbo].[P_RP_BaoTouRemissionCountMonthBB]    Script Date: 04/15/2011 10:43:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*《减免汇总月报表》中“本月减免”中的历史数据及“本年累计减免”中的数据应该分为本年数据和本年前数据。
《减免明细表》中“减免历史欠费”也应分为本年数据和本年前数据,目前的报表无法进行和财务的对账,
分开的目的就是为了和财务进行数据的核对*/
-- =============================================
-- Author:      zhengqiao@shanghai3h.com
-- Create date: <2009-12-3>
-- Description: <包头市供水总公司XXXX年XX月减免汇总月报表(T26)>
--------Sample:EXEC P_RP_BaoTouRemissionCountMonthBB '201002','','','',1ALTER procedure [dbo].[P_RP_BaoTouRemissionCountMonthBB](
@countDate varchar(20),--查询年月
@waterType varchar(20)='',--用水类别
@S_ST varchar(20)='',--站点,可复选用逗号隔开
@S_CID char(16)='',--用户代码
@flag int=1--分质用水标志位;0:分质;1:非分质
)
as
declare @sql varchar(3000)--临时sql变量,@NowMonth varchar(20)--当前账务年月,@JianMianTableName varchar(50)--ZW_JianMiann表,@JianMianZBTableName varchar(50)--ZW_JianMian_ZB表,@BiaoKaxxTableName VARCHAR(50) -- 表卡信息表名,@s_BeginDate varchar(100)--减免日期开始日期,@s_EndDate varchar(100)--减免日期结束日期,@condition varchar(1000)--拼接查询条件,@countYear varchar(50),@countMonth varchar(50),@exChange int--标志位
set @exChange=0
------------------------------------------------------------
set @countYear=substring(convert(char(8),@countDate,112),1,4)  --2011
set @countMonth=substring(convert(char(8),@countDate,112),5,2) --04-------得到账务年月范围---------
select @s_BeginDate=convert(varchar(10),D_KaiShiRQ,112),@s_EndDate=convert(varchar(10),D_JieShuRQ,112)
from SYS_JieZhuanRZ where I_ZhangWuNY=@countDate
--
print @s_BeginDate
if @s_BeginDate is nullset @exChange=1--return 0
--获取表名print  @countDate
select @JianMianTableName='ZW_JianMian'
select @JianMianZBTableName='ZW_JianMian_ZB'
select @BiaoKaxxTableName=dbo.F_GetTableName(@countDate,'KG_BiaoKaXX')
print @JianMianTableName------判断要用到的表中是否有不存在的IF(@JianMianTableName IS NULL)BEGINset @exChange=1--RETURN 0END
-----查询居民类别的简号
create table #TMPJMJH (I_JH INT ,I_TJH int)insert into #TMPJMJHselect * from F_BaotouGetFenLeiJH(0)
-----查询企业类别的简号
create table #TMPQYJH (I_JH INT,I_TJH int )insert into #TMPQYJHselect * from F_BaotouGetFenLeiJH(1)CREATE TABLE #TMPJMJH1 (I_JH INT,S_FeiLeiMC varchar(50))
insert into #TMPJMJH1 select I_JH,S_FeiLeiMC from (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) JG_YongShuiFL declare @S_JH varchar(1000)--存放简号,用逗号隔开
declare @JH varchar(20)--存放游标轮循的简号值
set @S_JH=''
if @waterType<>''begindelete from #TMPJMJH1--清空表set @sql='SELECT distinct I_JH,S_FeiLeiMC   FROM (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) [JG_YongShuiFL] where i_ancestor IN (SELECT ID FROM (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) JG_YongShuiFL  WHERE I_ANCESTOR =0) AND S_FEILEIMC LIKE ''%'+@waterType+'%'' 'print @sql   INSERT INTO #TMPJMJH1EXEC(@sql)---使用游标读取#TMPJMJH表中字段值declare JHCursor cursor for --声明游标select I_JH from #TMPJMJH1 open JHCursor --打开游标fetch next from JHCursor into @JH--使游标指向第一行--       print @JHwhile @@FETCH_STATUS=0beginset @S_JH=@S_JH+@JH+','fetch next from JHCursor into @JH           endclose JHCursor--关闭游标deallocate JHCursor--释放游标占用的空间--        print @S_JHif @S_JH<>''set @S_JH=substring(@S_JH,1,len(@S_JH)-1)end----拼接查询条件print '11111'set @condition=' and a.S_CaoZuoY=''100233'''if @S_ST<>'' and @S_ST<>'00'set @condition=@condition+' and a.S_ST in('+@S_ST+')'if @S_CID<>''set @condition=@condition+' and a.S_CID='''+@S_CID+''''if @waterType<>'' and @S_JH<>''set @condition=@condition+' and a.I_JH in('+@S_JH+')'select * into #biaoka from kg_biaokaxx where 1=2
set @sql='
select *  from '+@BiaokaxxTableName+'  where I_JiHuiYSJJFS>=0 '
insert into #biaoka exec(@sql)--定义报表临时表1  本月临时报表create table #NowMonth(I_JH int,WaterType varchar(50),--用水分类CountDate varchar(50),BasicFee numeric(12, 2),--基本水费SecondFee numeric(12,2),--二级水费OverstepPlanFee numeric(12,2),--超计划水费LateFee numeric(12,2),--滞纳金SewageDisposeFee numeric(12,2),--污水处理费TotalAmount numeric(12,2),--合计本月总额------------------------BasicFee1 numeric(12, 2),--历史基本水费SecondFee1 numeric(12,2),--历史二级水费OverstepPlanFee1 numeric(12,2),--历史超计划水费LateFee1 numeric(12,2),--滞纳金SewageDisposeFee1 numeric(12,2),--污水处理费TotalAmount1 numeric(12,2),--合计历史总额TotalCount numeric(12,2),--合计总额)--定义报表临时表2 本年临时报表create table #ThisYear(I_JH int,WaterType varchar(50),--用水分类TotalBasicFee numeric(12, 2),--基本水费TotalSecondFee numeric(12,2),--二级水费TotalOverstepPlanFee numeric(12,2),--超计划水费TotalLateFee numeric(12,2),--滞纳金TotalSewageDisposeFee numeric(12,2),--污水处理费YearTotalAmount numeric(12,2),--合计总额CountDate varchar(50),)
print '33333'
------------分质、非分质
declare @con varchar(100)if @flag=0set @con=' and c.I_BiaoZhongL=3'elseset @con=' and c.I_BiaoZhongL<>3'select * into #tmpJianMian from zw_jianmian where 1=2set @sql='select a.*from '+@JianMianTableName+' as a    left join '+@BiaoKaxxTableName+' as c on a.s_cid=c.s_cidwhere  (a.DL_CaoZuoRQ>='+@s_BeginDate+' and a.DL_CaoZuoRQ<='+@s_EndDate+')  and n_je>0and a.I_ChuLi in(4,16)  '+@con+@conditionprint @sqlinsert into #tmpJianMian exec(@sql)if @exChange=0begin----------------本月统计临时表----------------------基本水费
create table #tmpBasicFee(I_JH int,N_ZongJinE numeric(18,2))set @sql='select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE''  from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB=0 and b.i_leixing=1and b.I_FeiYongDLBH=580 and (a.i_y*100+a.i_m)='+@countDate+'group by b.I_JH'insert into #tmpBasicFee exec(@sql)--二级水费
create table #tmpSecondFee(I_JH int,N_ZongJinE numeric(18,2))set @sql='select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE''   from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB>0 and a.s_cid not in (select s_cid from #biaoka)and b.I_FeiYongDLBH=580 and b.i_leixing=1 and (a.i_y*100+a.i_m)='+@countDate+'group by b.I_JH'--print @sqlinsert into #tmpSecondFee exec(@sql)--超计划水费create table #tmpOverstepPlanFee(I_JH int,N_ZongJinE numeric(18,2))set @sql='select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE''   from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB>0 and a.s_cid  in (select s_cid from #biaoka)and b.I_FeiYongDLBH=580 and b.i_leixing=1 and (a.i_y*100+a.i_m)='+@countDate+'group by b.I_JH'--print @sqlinsert into #tmpOverstepPlanFee exec(@sql)-----滞纳金--set @sql='select a.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE'' into ##tmpLateFee from --#tmpJianMian as a --left join '+@JianMianZBTableName+' as b on --a.i_jianmianbh=b.i_jianmianbh --where a.I_JLZT=0  --and b.I_JLZT=0'+@condition+' --and (a.DL_CaoZuoRQ>='+@s_BeginDate+' and a.DL_CaoZuoRQ<='+@s_EndDate+') --and a.I_ChuLi in(4,16) --group by a.I_JH'----print @sql--exec(@sql)--污水处理费create table #tmpSewageDisposeFee(I_JH int,N_ZongJinE numeric(18,2))set @sql='select b.I_JH,sum(isnull(N_ZongJinE,0)) as ''N_ZongJinE''   from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on    a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB=0 and b.i_leixing=1and b.I_FeiYongDLBH=581 and (a.i_y*100+a.i_m)='+@countDate+'  group by b.I_JH' --print @sqlinsert into #tmpSewageDisposeFee exec(@sql)------------------------------------select I_JH into #tmp3 from #tmpBasicFee  --本月基本水费select I_JH into #tmp4 from #tmpSecondFee --二级水费select I_JH into #tmp5 from #tmpOverstepPlanFee --超计划水费select I_JH into #tmp6 from #tmpSewageDisposeFee --污水处理费--------------本月水费所有表--------------------------------           create table #first(i_jh int,s_feileimc varchar(64),BasicCost numeric(18,2),SecondLevelCost numeric(18,2),OverPlanCost numeric(18,2),LateFeeCost numeric(18,2),SlopsCost numeric(18,2),AllCost numeric(18,2), YM varchar(16))--------------合并本月水费所有表-----------------insert into #firstselect T.I_JH,F.S_FeiLeiMC,isnull(A.N_ZongJinE,0) as 'BasicFee',isnull(B.N_ZongJinE,0) as 'SecondFee',isnull(C.N_ZongJinE,0) as 'OverstepPlanFee',0 as 'LateFee',isnull(D.N_ZongJinE,0) as 'SewageDisposeFee',isnull(A.N_ZongJinE,0)+isnull(B.N_ZongJinE,0)+isnull(C.N_ZongJinE,0)+isnull(D.N_ZongJinE,0)as 'TotalAmount',@countYear+'年'+@countMonth+'月' as CountDatefrom (select I_JH from #tmp3 union select I_JH from #tmp4 union select I_JH from #tmp5 union select I_JH from #tmp6) Tleft join #tmpBasicFee A on T.I_JH=A.I_JHleft join #tmpSecondFee B on T.I_JH=B.I_JHleft join #tmpOverstepPlanFee C on T.I_JH=C.I_JHleft join #tmpSewageDisposeFee D on T.I_JH=D.I_JH --left join ##tmpLateFee E on T.I_JH=E.I_JH left join #TMPJMJH1 F on T.I_JH=F.I_JHend
-----------------------------------------------------------------------------------------------------------------------------------------------------减免历史基本水费、减免污水处理费---------
create table #lsBasicSlops(I_JH int,BasicCost numeric(18,2))
SET @SQL='SELECT t.*
FROM(
SELECT b.I_JH,SUM(b.N_ZongJinE) AS BasicCost
FROM #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB=0 and b.i_leixing=1and (a.i_y*100+a.i_m)<'+@countDate+' GROUP BY b.I_JH) t'
print @sql
insert into #lsBasicSlops EXEC(@SQL)
--污水费
create table #lsSlops(I_JH int,SlopsCost numeric(18,2))
SET @SQL='SELECT t1.*FROM (
SELECT b.I_JH,SUM(b.N_ZongJinE) AS SlopsCost
FROM #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=581 AND b.I_JIETIJB=0 and b.i_leixing=1and (a.i_y*100+a.i_m)<'+@countDate+'   GROUP BY b.I_JH
)  t1'
print @sql
insert into #lsSlops EXEC(@SQL)
----------减免历史二级水费---------------------
create table #lsSecondLevel(I_JH int,SecondLevelCost numeric(18,2))
SET @SQL='SELECT b.I_JH,SUM(b.N_ZongJinE) AS SecondLevelCost FROM  #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB>0 and b.i_leixing=1and (a.i_y*100+a.i_m)<'+@countDate+' and a.s_cid not in (select s_cid from #biaoka)GROUP BY b.I_JH'
insert into #lsSecondLevel EXEC(@SQL)
----------减免历史超计划水费-------------------
create table #lsOverPlan(I_JH int,OverPlanCost numeric(18,2))
SET @SQL='SELECT b.I_JH,SUM(b.N_ZongJinE) AS OverPlanCost
FROM  #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB>0 and b.i_leixing=1and (a.i_y*100+a.i_m)<'+@countDate+' and a.s_cid  in (select s_cid from #biaoka)GROUP BY b.I_JH'
insert into #lsOverPlan EXEC(@SQL)--------------------创建历史水费所有表
create table #second(i_jh int,s_feileimc varchar(64),BasicCost numeric(18,2),SecondLevelCost numeric(18,2),OverPlanCost numeric(18,2),LateFeeCost numeric(18,2),SlopsCost numeric(18,2),AllCost numeric(18,2), YM varchar(16))-----------------合并历史所有表-------------------
select distinct i_jh into #lsjh from #lsBasicSlops
union select i_jh  from #lsSecondLevel
union select i_jh  from #lsOverPlan
union select i_jh  from #lsSlops insert into #second SELECT DISTINCT t.i_jh,S_FeiLeiMC
,isnull(a.BasicCost,0) as BasicCost
,isnull(b.SecondLevelCost,0) as SecondLevelCost
,isnull(c.OverPlanCost,0) as OverPlanCost
,0 AS LateFeeCost
,isnull(e.SlopsCost,0) as SlopsCost
,ISNULL(a.BasicCost,0)+ISNULL(e.SlopsCost,0)+ISNULL(b.SecondLevelCost,0)+ISNULL(c.OverPlanCost,0) AS AllCost
, @countYear+'年'+@countMonth+'月' as CountDate
FROM   #lsjh  AS t
LEFT JOIN #lsBasicSlops AS a ON  t.I_JH=a.I_JH
LEFT JOIN #lsSecondLevel AS b ON t.I_JH=b.I_JH
LEFT JOIN #lsOverPlan AS c ON  t.I_JH=c.I_JH
LEFT JOIN #lsSlops AS e ON  t.I_JH=e.I_JH
LEFT JOIN (select * from JG_YongShuiFL where
i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) JG_YongShuiFL ON t.I_JH=JG_YongShuiFL.I_JH--#secon 历史水费所有表  #first本月水费所有表 #NowMonth 本月临时表(包含历史和非历史所有) #ThisYear 本年临时表 不含历史
----------------------------------------------------------------------------------------------------------------------------------------------本月临时统计
select distinct i_jh,S_FeiLeiMC,YM  into #he from #first
union select i_jh,S_FeiLeiMC,YM from #secondinsert into #NowMonth
select a.i_jh,a.S_FeiLeiMC,a.YM,
isnull(b.BasicCost,0) as BasicCost ,
isnull(b.SecondLevelCost,0) as SecondLevelCost,
isnull(b.OverPlanCost,0) as OverPlanCost,
isnull(b.LateFeeCost,0) as LateFeeCost ,
isnull(b.SlopsCost,0) as SlopsCost,
isnull(b.AllCost,0) as AllCost, --本月合计isnull(c.BasicCost,0) as BasicCost1,
isnull(c.SecondLevelCost,0) as SecondLevelCost1,--历史二级水费
isnull(c.OverPlanCost,0) as OverPlanCost1,--历史超计划水费
isnull(c.LateFeeCost,0) as LateFeeCost1,
isnull(c.SlopsCost,0) as SlopsCost1,
isnull(c.AllCost,0) as AllCost1, --本月历史减免合计isnull(b.AllCost,0)+isnull(c.AllCost,0) as Allcount
from #he as a
left join #first as b on a.i_jh=b.i_jh
left join #second as c on a.i_jh=c.i_jh---------------------本年统计临时表------------------
declare @year varchar(10),--当前时间的年份@m int,--月份循环变量@month int,--当前时间的月份@tmpYearM varchar(50),--循环使用账务年月@s_StartDate varchar(50),@I_EndDate varchar(50)
set @year=convert(char(4),@countDate,112)
set @month=cast(substring(convert(char(8),@countDate,112),5,2) as int)  --获取当前输入日期的月份
set @m=1
--print @month
-------------------------------------------------
---创建基本水费临时表
create table #tmpBasicFee1(I_JH int,N_ZongJinE numeric(12,2))
---创建二级水费临时表
create table #tmpSecondFee1(I_JH int,N_ZongJinE numeric(12,2))
---创建超计划水费临时表
create table #tmpOverstepPlanFee1(I_JH int,N_ZongJinE numeric(12,2))
---创建污水处理费临时表
create table #tmpSewageDisposeFee1(I_JH int,N_ZongJinE numeric(12,2))
---创建滞纳金临时表
--create table #tmpLateFee1(I_JH int,N_ZongJinE numeric(12,2))----------减免历史污水费
create table #ylsSlops(I_JH int,SlopsCost numeric(18,2))
----------减免历史基本水费
create table #ylsBasicSlops(I_JH int,BasicCost numeric(18,2))
----------减免历史二级水费
create table #ylsSecondLevel(I_JH int,SecondLevelCost numeric(18,2))
----------减免历史超计划水费
create table #ylsOverPlan(I_JH int,OverPlanCost numeric(18,2))---while循环遍历从1月到当前月的各种费用
while @m<=@month  --Condition here must be int or some type inherits intbeginif len(@m)=1set @tmpYearM=@year+'0'+cast(@m as varchar(5))elseset @tmpYearM=@year+cast(@m as varchar(5))print @tmpYearM-------得到账务年月范围---------select @s_StartDate=convert(varchar(8),D_KaiShiRQ,112),@I_EndDate=convert(varchar(8),D_JieShuRQ,112)from SYS_JieZhuanRZ where I_ZhangWuNY=@tmpYearMprint @s_StartDate--if @s_StartDate is nullbeginset @m=@m+1print @mcontinue--重新开始循环end--得到当前账务年月select @NowMonth=max(I_ZhangWuNY) from SYS_JieZhuanRZset @JianMianTableName='ZW_JianMian'set @JianMianZBTableName='ZW_JianMian_ZB'
select @BiaoKaxxTableName=dbo.F_GetTableName(@tmpYearM,'KG_BiaoKaXX')delete from #tmpJianMianset @sql='select a.*from '+@JianMianTableName+' as a  left join '+@BiaoKaxxTableName+' as c on a.s_cid=c.s_cidwhere  (a.DL_CaoZuoRQ>='+@s_StartDate+' and a.DL_CaoZuoRQ<='+@I_EndDate+')  and n_je>0and a.I_ChuLi in(4,16)  and (a.i_y*100+a.i_m)<='+@tmpYearM+@con+@conditioninsert into #tmpJianMian exec(@sql)--delete from #biaokaset @sql='select *  from '+@BiaokaxxTableName+'  where I_JiHuiYSJJFS>=0 ' insert into #biaoka exec(@sql)---基本水费set @sql='insert into #tmpBasicFee1 select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE'' from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB=0  and b.i_leixing=1 and b.I_FeiYongDLBH=580 group by b.I_JH'print @sqlexec(@sql)
print @s_BeginDate--合计本年二级水费set @sql='insert into #tmpSecondFee1 select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE'' from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB>0 and b.i_leixing=1 and a.s_cid not in (select s_cid from #biaoka)and b.I_FeiYongDLBH=580 group by b.I_JH'
--      print @sqlexec(@sql)--合计超计划水费set @sql='insert into #tmpOverstepPlanFee1 select b.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE'' from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB>0 and b.i_leixing=1 and a.s_cid  in (select s_cid from #biaoka)and b.I_FeiYongDLBH=580 group by b.I_JH'
--      print @sqlexec(@sql)
--      ---滞纳金
--      set @sql='insert into #tmpLateFee1
--      select a.I_JH,sum(b.N_ZongJinE) ''N_ZongJinE'' from
--      #tmpJianMian as a
--      left join '+@JianMianZBTableName+' as b on
--      a.i_jianmianbh=b.i_jianmianbh
--      where a.I_JLZT=0
--      and b.I_JLZT=0'+@condition+'
--      and (a.DL_CaoZuoRQ>='+@s_BeginDate+' and a.DL_CaoZuoRQ<='+@s_EndDate+')
--      and a.I_ChuLi in(4,16) group by a.I_JH'
----        print @sql
--      exec(@sql)--合计污水处理费set @sql='insert into #tmpSewageDisposeFee1 select b.I_JH,sum(isnull(b.N_ZongJinE,0)) as  ''N_ZongJinE'' from #tmpJianMian as a left join '+@JianMianZBTableName+' as b on a.i_jianmianbh=b.i_jianmianbh where a.I_JLZT=0 and b.I_JLZT=0 and b.I_JieTiJB=0 and b.i_leixing=1 and b.I_FeiYongDLBH=581  group by b.I_JH'
--      print @sqlexec(@sql)
----------------------------------------------------------历史减免--------------------------------------------------------SET @SQL='insert into #ylsBasicSlops SELECT t.*
FROM(
SELECT b.I_JH,SUM(b.N_ZongJinE) AS BasicCost
FROM #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB=0 and b.i_leixing=1 GROUP BY b.I_JH) t'
EXEC(@SQL) print @sqlSET @SQL='insert into #ylsSlops SELECT t1.*
FROM (
SELECT b.I_JH,SUM(b.N_ZongJinE) AS SlopsCost
FROM #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=581 AND b.I_JIETIJB=0 and b.i_leixing=1
GROUP BY b.I_JH
)  t1'
EXEC(@SQL) print @sqlSET @SQL='insert into #ylsSecondLevel SELECT b.I_JH,SUM(b.N_ZongJinE) AS SecondLevelCost
FROM  #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB>0 and b.i_leixing=1
and a.s_cid not in (select s_cid from #biaoka)GROUP BY b.I_JH'
EXEC(@SQL) print @sqlSET @SQL='insert into #ylsOverPlan SELECT b.I_JH,SUM(b.N_ZongJinE) AS OverPlanCost
FROM  #tmpJianMian AS a
LEFT JOIN '+@JianMianZBTableName+' AS b ON a.i_jianmianbh=b.i_jianmianbh
WHERE a.I_JLZT=0 and b.I_JLZT=0 AND b.I_FeiYongDLBH=580 AND b.I_JIETIJB>0 and b.i_leixing=1
and a.s_cid  in (select s_cid from #biaoka) GROUP BY b.I_JH'
EXEC(@SQL) print @sql
--------------------------------------------------------结束历史减免统计----------------------------------------------------------------------改变循环控制变量-----------set @m=@m+1
end
--------------------创建历史水费所有表
create table #ysecond(i_jh int,--s_feileimc varchar(64),BasicCost numeric(18,2),SecondLevelCost numeric(18,2),OverPlanCost numeric(18,2),LateFeeCost numeric(18,2),SlopsCost numeric(18,2),AllCost numeric(18,2), YM varchar(16))-----------------合并历史所有表-------------------
select distinct i_jh into #ylsjh from #ylsBasicSlops
union select i_jh  from #ylsSecondLevel
union select i_jh  from #ylsOverPlan
union select i_jh  from #ylsSlops insert into #ysecond SELECT DISTINCT t.i_jh
--,S_FeiLeiMC
,isnull(a.BasicCost,0) as BasicCost
,isnull(b.SecondLevelCost,0) as SecondLevelCost
,isnull(c.OverPlanCost,0) as OverPlanCost
,0 AS LateFeeCost
,isnull(e.SlopsCost,0) as SlopsCost
,ISNULL(a.BasicCost,0)+ISNULL(e.SlopsCost,0)+ISNULL(b.SecondLevelCost,0)+ISNULL(c.OverPlanCost,0) AS AllCost
, @countYear+'年'+@countMonth+'月' as CountDate
FROM #ylsjh  AS t
LEFT JOIN #ylsBasicSlops AS a ON  t.I_JH=a.I_JH
LEFT JOIN #ylsSecondLevel AS b ON t.I_JH=b.I_JH
LEFT JOIN #ylsOverPlan AS c ON  t.I_JH=c.I_JH
LEFT JOIN #ylsSlops AS e ON  t.I_JH=e.I_JH
LEFT JOIN (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) JG_YongShuiFL ON t.I_JH=JG_YongShuiFL.I_JH--创建临时表 保存 #ysecond中部分内容
create table #tmpsec(   i_jh int,yBasicCost numeric(18,2),ySecondLevelCost numeric(18,2),yOverPlanCost numeric(18,2),yLateFeeCost numeric(18,2),ySlopsCost numeric(18,2))insert into #tmpsec
select i_jh, sum(BasicCost),sum(SecondLevelCost),sum(OverPlanCost),sum(LateFeeCost),sum(SlopsCost) from #ysecond group by i_jh
print 'ssssssss'
------------------------------------------------结束历史所有表----------------------------------------------------------------select I_JH,sum(N_ZongJinE) 'N_ZongJinE' into #tmpBasic from #tmpBasicFee1 group by I_JHselect I_JH,sum(N_ZongJinE) 'N_ZongJinE' into #tmpSecond from #tmpSecondFee1 group by I_JHselect I_JH,sum(N_ZongJinE) 'N_ZongJinE' into #tmpOverstepPlan from #tmpOverstepPlanFee1 group by I_JHselect I_JH,sum(N_ZongJinE) 'N_ZongJinE' into #tmpSewageDispose from #tmpSewageDisposeFee1 group by I_JH--   select I_JH,sum(N_ZongJinE) 'N_ZongJinE' into #tmpLate
--      from #tmpLateFee1 group by I_JHselect I_JH into #tmp31 from #tmpBasicselect I_JH into #tmp41 from #tmpSecondselect I_JH into #tmp51 from #tmpOverstepPlanselect I_JH into #tmp61 from #tmpSewageDispose
--    select I_JH into #tmp71 from #tmpLate-----------------------年统计---------------------------insert into #ThisYearselect T.I_JH,F.S_FeiLeiMC,--用水分类isnull(A.N_ZongJinE,0) as 'TotalBasicFee', --基本水费isnull(B.N_ZongJinE,0) as 'TotalSecondFee', --二级水费isnull(C.N_ZongJinE,0) as 'TotalOverstepPlanFee',--超计划水费0 as 'TotalLateFee',  ---滞纳金isnull(D.N_ZongJinE,0) as 'TotalSewageDisposeFee',--污水处理费isnull(A.N_ZongJinE,0)+isnull(B.N_ZongJinE,0)+isnull(C.N_ZongJinE,0)+isnull(D.N_ZongJinE,0) as 'YearTotalAmount', --本年合计减免@countYear+'年'+@countMonth+'月' as CountDatefrom (select I_JH from #tmp31 union select I_JH from #tmp41 union select I_JH from #tmp51 union select I_JH from #tmp61)Tleft join #tmpBasic A on T.I_JH=A.I_JHleft join #tmpSecond B on T.I_JH=B.I_JHleft join #tmpOverstepPlan C on T.I_JH=C.I_JHleft join #tmpSewageDispose D on T.I_JH=D.I_JH left join #TMPJMJH1 F on T.I_JH=F.I_JHleft join #ysecond G on T.I_JH=G.I_JH--  left join #tmpLate E on T.I_JH=E.I_JH----------------------------------------------select  distinct i_jh,CountDate,WaterType into  #jh  from #NowMonth union select i_jh,CountDate,WaterType from #ThisYear---------------------------------------------------------------------------------------
if @exChange=0beginselect  distinctf.s_feileimc as s_daleimc,   --用户类别t. CountDate,  --统计年月t.WaterType,  --用水类别isnull(a.BasicFee,0) 'BasicFee',  --本月基本水费isnull(a.SecondFee,0) 'SecondFee', --本月二级水费isnull(a.OverstepPlanFee,0) 'OverstepPlanFee',--本月超计划水费isnull(a.LateFee,0) 'LateFee',  --本月滞纳金isnull(a.SewageDisposeFee,0) 'SewageDisposeFee', --本月污水处理费isnull(a.TotalAmount,0) 'TotalAmount',  --本月合计isnull(a.BasicFee1,0) 'BasicFee1',  --历史基本水费isnull(a.SecondFee1,0) 'SecondFee1', --历史二级水费isnull(a.OverstepPlanFee1,0) 'OverstepPlanFee1',--历史超计划水费isnull(a.LateFee1,0) 'LateFee1', --历史滞纳金isnull(a.SewageDisposeFee1,0) 'SewageDisposeFee1', --历史污水处理费isnull(a.TotalAmount1,0) 'TotalAmount1', --历史合计isnull(a.TotalCount,0) as TotalCount, --减免合计b.TotalBasicFee,   --基本水费b.TotalSecondFee,  --二级水费b.TotalOverstepPlanFee, --超计划水费b.TotalLateFee, --滞纳金b.TotalSewageDisposeFee, --污水处理费------------c.yBasicCost, c.ySecondLevelCost,c.yOverPlanCost,c.yLateFeeCost,isnull(c.ySlopsCost,0) 'ySlopsCost',--0 as SlopsCost,--c.AllCost ,ISNULL(c.yBasicCost,0)+ISNULL(c.ySecondLevelCost,0)+ISNULL(c.yLateFeeCost,0)+ISNULL(c.yLateFeeCost,0)+ISNULL(c.ySlopsCost,0) AS AllCost,----------------------b.YearTotalAmount --合计from #jh as t left join #NowMonth as a on t.i_jh=a.i_jhleft join #ThisYear as b on t.I_JH=b.I_JHleft join #tmpsec as c on  t.I_JH=c.I_JHleft JOIN (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) JG_YongShuiFL ON t.I_JH=                 JG_YongShuiFL.I_JHleft join (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) f on JG_YongShuiFL.I_ANCESTOR=f.IDend
elsebeginselect distinct f.s_feileimc as s_daleimc ,b.WaterType,a.*,b.CountDate,b.TotalBasicFee,b.TotalSecondFee,b.TotalOverstepPlanFee,b.TotalLateFee,b.TotalSewageDisposeFee,----------------c.yBasicCost as 'yBasicCost',c.ySecondLevelCost as'ySecondLevelCost',c.yOverPlanCost as'yOverPlanCost',c.yLateFeeCost as 'yLateFeeCost',c.ySlopsCost as 'ySlopsCost',--0 as SlopsCost,--c.AllCost,ISNULL(c.yBasicCost,0)+ISNULL(c.ySecondLevelCost,0)+ISNULL(c.yLateFeeCost,0)+ISNULL(c.yLateFeeCost,0)+ISNULL(c.ySlopsCost,0) AS AllCost,b.YearTotalAmountfrom #NowMonth as a right join #ThisYear as b on a.I_JH=b.I_JH right join #tmpsec as c on  b.I_JH=c.I_JHLEFT JOIN (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia ))JG_YongShuiFL ON b.I_JH=JG_YongShuiFL.I_JHleft join (select * from JG_YongShuiFL where i_tiaojiah=(select max(i_tiaojiah) from jg_tiaojia )) f on JG_YongShuiFL.I_ANCESTOR=f.IDenddrop table #NowMonthdrop table #ThisYeardrop table #tmpJianMian

转载于:https://www.cnblogs.com/zq281660880/archive/2011/04/15/2017272.html

做报表时用到的一个存储过程相关推荐

  1. 做报表必看!一个工具就可搞定中国式复杂报表需求

    中国式报表?对于做财务的"表哥表姐"肯定是不陌生的,但是对于大多数做报表的萌新来说,应该会提出灵魂三问:什么是中国式报表?中国式报表有什么特点?玩不转excel该如何制作中国式报表 ...

  2. 从腾讯入职到离职,我仅用了三周:做大数据的同事看不起做报表的

    这是很多年前的事情了,从腾讯入职到离职,我用了三周,理由很简单,做大数据的同事看不起做报表的,当然,我是做报表的那个. 做大数据的,就一定能做好报表吗? 报表是企业IT数据建设必不可少的一环,小到一张 ...

  3. 混合模式程序matlab,VS2013 C# 调用 cognex 的QuickBuild做程序时发生一个错误

    今天在用 VS2013 C# 调用 cognex 的QuickBuild做程序时发生一个错误,如下所示 混合模式程序集是针对"v2.0.50727"版的运行时生成的,在没有配置其他 ...

  4. 水晶报表技术(12)——一个投票系统水晶报表应用

        前一段时间,公司需要做一个在线的调查投票系统,目的是统计公司能力等级指数,统计的能力级别分三大类,分别为一级能力要素,二级能力要素,三级能力要素,不同的一级能力要素下面分若干的二级能力要素,每 ...

  5. 年薪60w财务总监的加薪秘诀:从不用Excel做报表

    都2021年了,已经是数字化转型的时代了,竟然还有企业在用Excel做报表? 在Boss招聘上花了几千块钱招人,统一格式是:"精通Excel,要求会使用数据透视表.Vlookup等函数,完成 ...

  6. 做报表5年,被批Excel毫无新意,能开发的可视化报表让我重获新生

    本文素材来自于真实粉丝 从小我就对数据感兴趣,还记得大学课堂上信息化老师在解读淘宝双十一数据,精准分析,资源合理分配.数据分析的强大魅力,带来经济效益的显著提升,给我开启了一个新世界的大门,确实妙不可 ...

  7. 做报表的朋友偷偷告诉我月薪5w的秘密:让报表动起来

    做过报表的人,尤其是经常有报表需求的人,想必都会有这样一个困惑: 为什么同样是做报表,其他人的报表就能够得到领导的肯定,而自己的报表就会被老板丢掉一边? 这个问题其实很好回答,你想一想,如果你是老板, ...

  8. 做报表到10点才下班,做的还是丑,怎样才能做出一张好看的报表?

    点了进来,说明你也有做报表的烦恼. 做报表这件事外人听起来简单,但只有做过的人才知道,真正做起来既要考虑到功能还有美观度,好不容易做完了,看着手指头都能数过来的访客数,顿时心累,觉得工作成果根本无人问 ...

  9. php实现报表拖拉拽,看!这款BI工具用拖拉拽的方式做报表

    原标题:看!这款BI工具用拖拉拽的方式做报表 怎样的BI工具才算容易上手?那就非拖拉拽式的OurwayBI智能BI工具莫属了.这款由资深BI老厂商自主开发的智能BI工具操作简单,会拖拉拽就能做BI报表 ...

最新文章

  1. pxe linux 配置文件,Linux PXE 部署
  2. 全志linux led驱动程序,芯灵思Sinlinx A64 linux通过设备树写LED驱动(附参考代码,未测试)...
  3. Linux RHCS中心跳网络接口的指定
  4. AI实战!开源一个机器学习/情感分析实战项目(附源码/教程)|湾区人工智能...
  5. 二分查找算法(C#实现)
  6. 超越村后端开发(7:修改完善代码(持续更新))
  7. linux内核的冷热页分配器
  8. 首个完全武器化的 Spectre Exploit 现身
  9. hdu 1209 clocks wrong answer 我的错误代码(没审好题唉,角度一样后,还要按小时排序。...
  10. 在 WPF 程序中使用矢量图
  11. 指令重排序导致的可见性问题
  12. 零基础数据挖掘入门-二手车价格预测part(一):EDA-数据探索性分析
  13. Android关闭屏幕,但不休眠
  14. 专业导师告诉你,有哪些51单片机教程值得大力推荐
  15. 感谢csdn 2021年中秋月饼礼盒
  16. UVA1445 Cubist Artwork
  17. centos 中文乱码_StudingThinking_百度空间
  18. win7系统修复工具_联想出品30多款系统快速修复工具,为你的电脑保驾护航!
  19. Python实现商品价格监控,识破双十一的套路
  20. yxy小蒟蒻的201112总结

热门文章

  1. mysql 高级语法,SQL 语法高级
  2. BFS Sicily 1215: 脱离地牢
  3. 2.2.1 mini-batch
  4. mysql执行出错:Table 'k_user' is read only
  5. Python学习笔记(六)if判断语句
  6. hosts 持续更新 - laod
  7. Angular.js
  8. 修改hostname
  9. asp.net的几个错误
  10. c# equals与==的区别【转】