日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

做报表时用到的一个存储过程

發布時間:2025/4/5 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 做报表时用到的一个存储过程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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

總結

以上是生活随笔為你收集整理的做报表时用到的一个存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。