绘图项目的累积结果

时间:2014-02-07 09:56:32

标签: sql sql-server sql-server-2008 tsql

我有一个表ENG_Drawing.Its字段是

SELECT DrawingID,DrawingNumber,DrawingDescription FROM ENG_Drawing 

DrawingID        DrawingNumber      DrawingDescription   etc..
1131            R152-14-01-00-31       DU - 01              etc..    
....
1200            R160-02-01-00-21       DU - 12              etc..        
....
.....
....
1500            R156-01-01-00-11       DU - 00              etc..
....
.....
2993        R167-09-01-00-21       DU - 04              etc..
2994        R165-10-01-00-40       DU - 25              etc..
2995        R163-13-01-00-01       DU - 24              etc..
2996        R100-12-01-00-61       DU - 21              etc..
2997        R166-09-01-00-21       DU - 21              etc..
2998        R125-14-01-00-02       DU - 32              etc..
2999
3000
 .
 .

绘图表的详细信息表是

DrawingID   SeqNbr  Drawing_f   BOMQuantity    ItemQty        DrawingreferenceID   ItemID
1131        7       0           2               0.574                           1231
1131        9       1           4               null        1200                null
....        
....
1200        14      1           8               0.560       1100                1555
1200        16      1           2               5.23        1500                1755
....
....
1500        18      0           2               4.350                           2684
....
2993        5       0           2               0.061                           2125
2993        6       0           2               1.592                           1698
2993        7       0           2               2.500                           1231
2993        9       1           4               null        1131                null
2993        16      1           2               null        1500                null
2993        18      0           2               4.350                           2684

当我将DrawingID作为2993时 需要这样的输出

ITEMID  TOTALQTY
1231    9.592  //select((2*2.500) + (4*2*.574)) here (2*2.500)- is BOMQuantity(4)*ItemQty(2.500) for ItemID 2993 and (4*2*.574)- BOMQuantity of DrawingID 1131(4)*BOMQuantity of ItemID 1231 in DRawingID 1131(2)*ItemQty(.574)
1555    71.680  //select(4*4*8*.560) here 4-BOMQuantity of DrawingreferenceID 1131 in DrawingID 2993.Middle 4 is BOMQuantity of DrawingreferenceID 1200 in DrawingID 1131. 8 is  BOMQuantity of ItemId 1555 in DrawingId 1200
1698    3.184  //select(2*1.592)
1755    50.944  //select(4*4*2*1.592)
2125    0.122  //select(2*0.061)
2684    0.122  //select((2*4.350)+(2*2*4.350))

ENG_Drawing_BOM包含子图(DrawingreferenceID)。他们(DrawingreferenceID)在ENG_Drawing表中有它们的值,还有项目和子图。 一张图可能没有子图,而子图可以有子图等等......

我需要当我提供DrawingID时,我需要项目明智的累积。结果应该是ITEMID,sum(BOMQuantity * ItemQty)为TOTALQTY。 问题是, 例如  DrawingID有4个BOMQuantity的绘图,我需要TOTALQTY 4倍的绘图(S1)项目,  如果那个子图有子图(S2),其中6个BOMQuantity需要4个S1项,4 * 6个4个S2项......它将继续为其内层......

我终于需要整体ITEMID明智的累积

这里2993为主要图纸.1131,1200,1500是子图纸,子图纸可能有子图纸。我也需要它们。

任何身体????

2 个答案:

答案 0 :(得分:1)

@Suresh重新检查你的样本数据,我的查询有什么问题?

Declare @drawing table( DrawingID int,SeqNbr int,Drawing_f int,BOMQuantity decimal(5,2),ItemQty decimal(6,3),DrawingreferenceID int,ItemID int)
insert into @drawing
select 1131 ,       7,       0,2, 0.574,null,1231 union all
select 1131 ,       9,       1,4,null ,1200,null union all
select 1200,        14,      1,8,0.560,       1100,1555 union all
select 1200,        16,      1,2,5.23 ,       1500,1755 union all
select 1500,        18,      0,2,4.350,       null,2684 union all
select 2993,        5,       0,2,0.061,         null,2125 union all
select 2993,        6,       0,2,1.592,          null,1698 union all
select 2993,        7,       0,2,2.500,         null,1231 union all
select 2993,        9,       1,4,null ,       1131,null union all
select 2993,        16,      1,2,null ,       1500,null union all
select 2993,        18,      0 ,2,4.350,         null,2684 
--select  * from @drawing 
;with cte as
(select  * ,1 roots from @drawing where DrawingID=2993 
union all
select d.*,case when d.DrawingID=c.DrawingreferenceID then roots+1 else roots end from @drawing d inner join cte c on d.DrawingID=c.DrawingreferenceID 


)
select   * from cte

@Suresh,如果您的上一次查询没问题,那么请准确

with cte as
(
    select  d.* , 1 as Nooff,1 roots,d.DrawingID as 'MainDrawing' from ENG_Drawing_BOM  d 
    union all
    select d.*,c.BOMQuantity*C.Nooff  as Nooff,case when d.DrawingID=c.DrawingreferenceID then roots+1 else roots end ,MainDrawing as 'MainDrawing' 
    from ENG_Drawing_BOM d inner join cte c on d.DrawingID=c.DrawingreferenceID 
)
,CTE1 as
(
select 
    cte.MainDrawing,
    cte.ItemID, 

    SUM(Nooff*cte.UnitWeight)as 'Wt',
    SUM((TotalQty)*Nooff)  as 'TotalQty'
from cte

where DrawingReferenceID is   null AND MainDrawing = 2993
group by cte.ItemID, cte.MainDrawing


)
select 
    cte.MainDrawing,
    cte.ItemID, 
    i.ItemDescription,
     Wt,
    TotalQty
from cte1
    inner join STR_Item i on cte.ItemID = i.ItemID 
where DrawingReferenceID is   null AND MainDrawing = 2993

    ORDER BY cte.ItemID

答案 1 :(得分:0)

这确实需要......来自KumarHarsh的回复。

with cte as
(
    select  d.* , 1 as Nooff,1 roots,d.DrawingID as 'MainDrawing' from ENG_Drawing_BOM  d 
    union all
    select d.*,c.BOMQuantity*C.Nooff  as Nooff,case when d.DrawingID=c.DrawingreferenceID then roots+1 else roots end ,MainDrawing as 'MainDrawing' from ENG_Drawing_BOM d inner join cte c on d.DrawingID=c.DrawingreferenceID 
)



select 
    cte.MainDrawing,
    cte.ItemID, 
    i.ItemDescription,
    SUM(Nooff*cte.UnitWeight)as 'Wt',
    SUM((TotalQty)*Nooff)  as 'TotalQty'
from cte
    inner join STR_Item i on cte.ItemID = i.ItemID 
where DrawingReferenceID is   null AND MainDrawing = 2993
group by cte.ItemID,
    cte.MainDrawing,
    i.ItemDescription
    ORDER BY cte.ItemID