添加列以获取递归sql查询中的值的总和

时间:2015-08-13 09:54:15

标签: sql sql-server sql-server-2008-r2

这是一个递归查询,用于获取有关零件及其子零件的详细信息。

DECLARE @PartNo Char(22)
DECLARE @PartIssue AS CHAR(4)
DECLARE @Level AS INT
DECLARE @PartType AS CHAR(12)
DECLARE @TempLeadTime AS FLOAT
DECLARE @CumLeadTime AS FLOAT 
SELECT @PartIssue = drawissno, @PartNo = partnum, @Level=1, @PartType = sm FROM partmaster  where partnum = 'AE40-0287810'

WITH Hierarchyct(BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty) AS

      (SELECT     @Level AS BOMLevel,
                        @PartNo AS bomchild, 
                        @PartType as parttype,
                        cast('' AS CHAR(22)) AS bomparent,  
                        cast(1.0 AS FLOAT) AS bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = @PartNo) AS ldtime,
                        @PartIssue as childissue, 
                        eccbom AS bmethod, 
                        eccroute as rmethod,
                        cast(1.0 AS FLOAT) AS requiredqty

      FROM eccissue 
      WHERE eccpart = @PartNo AND eccissueno = @PartIssue 

      UNION ALL

      SELECT            (hierarchyct.BOMLevel + 1) AS BOMLevel,
                        bom.bomchild, 
                        (SELECT sm from partmaster where partnum = bom.bomchild) as parttype,
                        bom.bomparent, 
                        bom.bomqty, 
                        (SELECT stleadtim FROM stock WHERE stocknum = bom.bomchild) as ldtime,

                        (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AS childissue,
                        bom.baltmethod AS bmethod,    
                        (SELECT eccroute FROM eccissue WHERE eccissue.eccissueno = (CASE WHEN LTRIM(RTRIM(bom.bomissue)) = 'CURR' THEN (SELECT drawissno FROM PartMaster WHERE PartNum = bom.bomchild) ELSE bom.bomissue END) AND eccissue.eccpart = bom.bomchild ) as rmethod,
                        (bom.bomqty * hierarchyct.requiredqty) AS requiredqty

      FROM bom  INNER JOIN hierarchyct ON bom.bomparent = hierarchyct.bmchild 


      WHERE bom.baltmethod =(SELECT eccbom FROM eccissue WHERE eccissue.eccpart = hierarchyct.bmchild AND eccissue.eccissueno = hierarchyct.childissue )
      )

SELECT   hierarchyct.*,
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.bmqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtime, 
CASE WHEN (stleadtim = 0 AND partmaster.sm = 'MANUFACTURED') THEN (Hierarchyct.requiredqty*sum(routing.runtim)) ELSE stleadtim  END AS ldtimeforRQ 
FROM    hierarchyct left outer join routing on part = bmchild and raltmethod = rmethod left outer join stock on stocknum = bmchild  left outer join partmaster on partnum = Hierarchyct.bmchild 
GROUP BY BOMLevel,bmchild, parttype, bomparent, bmqty, leadtime, childissue,bmethod,rmethod, requiredqty, stleadtim, sm, part

我得到了如下结果。

query result

我需要将ldtime列中所有值的总值变为另一列。这样做的原因是计算给定父部件的总提前期。请注意,必须添加所有提前期。将总值设置在另一行底部的单行中就足够了。如何实现这一目标?

请告知如何执行此操作。我正在使用SQL Server 2008 R2。

0 个答案:

没有答案