set nocount on; declare @a int declare @b int set @a=${fsyear} set @b=${searchrq} IF OBJECT_ID(N'tempdb.dbo.#jwl_ycl_zzj_daizhi_je') IS NOT NULL BEGIN DROP TABLE #jwl_ycl_zzj_daizhi_je END CREATE TABLE #jwl_ycl_zzj_daizhi_je ( wldlname NVARCHAR(200), fworkname NVARCHAR(200), fstockresid int, fpddate DATETIME, FCompanyID INT , fyear INT, fmonth INT, fcompanyname NVARCHAR(200), llrq DATETIME, je DECIMAL(18,6) ) INSERT #jwl_ycl_zzj_daizhi_je EXEC jwlck_daizhi @a,@b INSERT #jwl_ycl_zzj_daizhi_je EXEC yclck_daizhi @a,@b INSERT #jwl_ycl_zzj_daizhi_je EXEC zzjck_daizhi @a,@b ;WITH tb AS ( SELECT CASE b.FCompanyID WHEN 21 THEN 1 WHEN 98 THEN 2 WHEN 86 THEN 3 WHEN 64 THEN 4 WHEN 116 THEN 5 WHEN 117 THEN 6 WHEN 157 THEN 7 WHEN 45 THEN 8 WHEN 85 THEN 9 ELSE 10 END orderno, wldlname , YEAR(fpddate)fyear , MONTH(fpddate)fmonth, b.FCompanyID , a.FCompanyName, wldlname as categoryname,b.je FROM #jwl_daizhi_je b left join Bd_Company a on a.FCompanyID=b.FCompanyID WHERE 1=1 ) SELECT tb.fyear , tb.fmonth , tb.FCompanyID , tb.FCompanyName, SUM(tb.je)je,tb.orderno FROM tb WHERE 1=1 AND tb.FCompanyID IN (21,98,86,64,116,117,157,85,45) ${if(len(FCompanyID)==0,""," and tb.FCompanyName in ('"+FCompanyID+"') ")} --tb.fmonth<=MONTH(GETDATE()) GROUP BY tb.fyear , tb.fmonth , tb.FCompanyID , tb.FCompanyName, tb.orderno ORDER BY tb.orderno,tb.fyear,tb.fmonth |