标签:sele tin data char 合并 rop table ansi arc
USE [UFDATA_001_2017] GO /****** Object: StoredProcedure [dbo].[PROC_JDS_preorder] Script Date: 07/17/2018 16:50:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO CREATE PROCEDURE [dbo].[PROC_JDS_preorder] @cusname varchar(20)=null,@itemcode varchar(20)=null AS BEGIN --如果临时表存在就删除 --select AAAA=@cusname,BBBB=@itemcode IF OBJECT_ID('tempdb..#jds_20180713_1') is not null drop table #jds_20180713_1 IF OBJECT_ID('tempdb..#jds_20180713_2') is not null drop table #jds_20180713_2 --构建明细发货数据及数据特征(包含退货单数据,退货单数据直接和发货单合并) --临时表 #jds_20180713_1 SELECT a.ddate, a.ccuscode, a.ccusabbname, a.cinvcode, a.cinvname,a.cinvstd, a.inum, b.minddate, b.addhalfayear, b.addayear, CASE WHEN a.ddate = b.minddate THEN 1 WHEN a.ddate > b.minddate AND a.ddate <= b.addhalfayear THEN 2 WHEN a.ddate > b.addhalfayear AND a.ddate <= b.addayear THEN 3 ELSE 4 END AS salesign into #jds_20180713_1 FROM dbo.Sa_SaleOrderAscendDis AS a INNER JOIN (SELECT MIN(ddate) AS minddate, DATEADD(mm, 6, MIN(ddate)) AS addhalfayear, DATEADD(mm, 12, MIN(ddate)) AS addayear, ccuscode, cinvcode FROM dbo.Sa_SaleOrderAscendDis AS Sa_SaleOrderAscendDis_1 GROUP BY ccuscode, cinvcode) AS b ON a.ccuscode = b.ccuscode AND a.cinvcode = b.cinvcode -- 构建【客户+产品】首次、半年、全年汇总发货数据 --临时表#jds_20180713_2 select ccuscode,ccusabbname,cinvcode,cinvname,cinvstd,minddate,SUM(case when salesign=1 then inum else 0 end) firstinum, addhalfayear,SUM(case when salesign=1 or salesign=2 then inum else 0 end) halfyearinum, addayear,SUM(case when salesign=1 or salesign=2 or salesign=3 then inum else 0 end) ayearinum into #jds_20180713_2 from #jds_20180713_1 group by ccuscode,ccusabbname,cinvcode,cinvname,cinvstd,minddate,addhalfayear,addayear --关联预测单并求差值 select a.cCusCode 客户编码,a.cCusName 客户名称,a.cCode 预测单号,a.dDate 开户日期,b.cInvCode 产品编码,d.cinvname 产品名称,d.cinvstd 产品规格,b.iNum 预首购件,b.cDefine34 预半年购件,b.cDefine35 预全年购件, isnull(c.minddate,'无发货') 首发日,isnull(c.firstinum,0) 首发件,cast((isnull(c.firstinum,0)/b.iNum) as decimal(4,2)) 首发比,(isnull(c.firstinum,0)-b.iNum) 首发差,c.addhalfayear 半年日,isnull(c.halfyearinum,0) 半年发件,cast((isnull(c.halfyearinum,0)/b.cDefine34) as decimal(4,2)) 半年比,(isnull(c.halfyearinum,0)-b.cDefine34) 半年差, c.addayear 全年日,isnull(c.ayearinum,0) 全年发件,cast(ISNULL(c.ayearinum,0)/b.cDefine35 as decimal(4,2)) 全年比,(isnull(c.ayearinum,0)-b.cDefine35) 全年差 from SA_PreOrderMain a INNER JOIN SA_PreOrderDetails b ON a.ID=b.ID inner join inventory d on b.cInvCode=d.cInvCode left join #jds_20180713_2 c on a.cCusCode=c.ccuscode and b.cinvcode=c.cInvCode where 1=1 and (a.cCusName like '%'+@cusname+'%' or @cusname is null ) and (b.cInvCode like '%'+@itemcode+'%' or @itemcode is null) drop table #jds_20180713_1 drop table #jds_20180713_2 END GO标签:sele tin data char 合并 rop table ansi arc
原文地址:http://blog.51cto.com/13866219/2145931