使用左连接求和值

时间:2017-12-08 21:47:48

标签: sql-server sql-server-2008 left-join

我试图总结材料的数量,并将其与轮班中生产的零件放在一起。我的想法是使用左连接来做我想做的事情,但我做错了。我已经提供了一个我从中提取信息的数据片段。

表01

Location    Shift   Prod_Date   Total_Parts_Produced
Converter021    1   9/1/2017    270
Converter023    3   9/4/2017    300
Converter021    1   9/5/2017    500
Converter023    3   9/5/2017    486
Converter021    1   9/6/2017    300
Converter023    3   9/6/2017    486
Converter021    1   9/7/2017    500
Converter023    3   9/10/2017   600
Converter021    1   9/11/2017   600
Converter023    3   9/11/2017   486
Converter021    1   9/12/2017   630
Converter023    3   9/12/2017   486
Converter021    1   9/13/2017   500
Converter023    3   9/13/2017   600
Converter021    1   9/14/2017   391
Converter023    3   9/14/2017   500

表02

Location    Date_Created    Quantity
Converter021    9/1/2017    39
Converter021    9/1/2017    20
Converter021    9/1/2017    13
Converter021    9/1/2017    3
Converter021    9/1/2017    39
Converter021    9/9/2017    59
Converter021    9/11/2017   1
Converter021    9/11/2017   3
Converter021    9/12/2017   13
Converter021    9/12/2017   21
Converter021    9/14/2017   3
Converter021    9/14/2017   12
Converter021    9/14/2017   2
Converter023    9/15/2017   38
Converter023    9/15/2017   36
Converter023    9/15/2017   116 


SELECT
    si.Location
    ,si.Shift
    ,si.Prod_Date
    ,si.Total_Parts_Produced AS 'Parts Produced'
    ,SUM(b.Quantity)         AS 'Total'
FROM Table01      si
LEFT JOIN Table02 a ON si.Location = a.Location
LEFT JOIN Table02 b ON si.Prod_Date = b.Date_Created
WHERE Line = 'Converter 2'
    AND Prod_Date >= '09-01-2017'
    AND Prod_Date <= '09-17-2017'
GROUP BY Prod_Date
    ,si.Work_Center
    ,Shift
    ,Total_Parts_Produced
ORDER BY Prod_Date
    ,Shift ASC; 

预期结果

Location    Shift   Prod_Date   Total_Parts_Produced    Total
Converter021    1   9/1/2017    270         114
Converter023    3   9/4/2017    300         0
Converter021    1   9/5/2017    500         0
Converter023    3   9/5/2017    486         0
Converter021    1   9/6/2017    300         0
Converter023    3   9/6/2017    486         0
Converter021    1   9/7/2017    500         0
Converter023    3   9/10/2017   600         0
Converter021    1   9/11/2017   600         4
Converter023    3   9/11/2017   486         0
Converter021    1   9/12/2017   630         34
Converter023    3   9/12/2017   486         0
Converter021    1   9/13/2017   500         0
Converter023    3   9/13/2017   600         0
Converter021    1   9/14/2017   391         17
Converter023    3   9/14/2017   500         0
Converter023    3   9/17/2017   600         0

这是我运行查询时得到的结果。我无法弄清楚出了什么问题。

代码结果

Location    Shift   Prod_Date   Total_Parts_Produced    Total
Converter021    1   9/1/2017    270         2853600
Converter023    3   9/4/2017    300         NULL
Converter021    1   9/5/2017    500         1000500
Converter023    3   9/5/2017    486         92000
Converter021    1   9/6/2017    300         533745
Converter023    3   9/6/2017    486         49080
Converter021    1   9/7/2017    500         566805
Converter023    3   9/10/2017   600         800
Converter021    1   9/11/2017   600         765600
Converter023    3   9/11/2017   486         70400
Converter021    1   9/12/2017   630         691215
Converter023    3   9/12/2017   486         63560
Converter021    1   9/13/2017   500         622485
Converter023    3   9/13/2017   600         57240
Converter021    1   9/14/2017   391         1424625
Converter023    3   9/14/2017   500         131000
Converter023    3   9/17/2017   600         NULL

0 个答案:

没有答案