使用GROUP BY将SQL查询写入总记录

时间:2019-02-06 17:53:34

标签: mysql sql

我正在查询mc.Amount应该是一条记录的数据,但是我发现用户已经在每个订单号上添加了多条记录。 下面的代码给了我我的记录,但是我在确定如何计算oh.Order_Number的mc.Amount时遇到了问题。

除了mc.Amount

以外的所有字段都相同

我相信我需要一个子查询,但是我无法使其工作。任何帮助将不胜感激。

SELECT DISTINCT 
oh.Order_Number AS Order_Number,
od.Part_Number AS Job_Name,
od.Unit_Price AS Sale_Price,
mc.Amount AS PieceRate_Labor_Cost,
oh.Total_Labor_Cost AS Hourly_Labor_Cost,
oh.Total_Material_Cost AS Total_Material_Cost,
SUM(oh.Total_Labor_Cost + oh.Total_Material_Cost+ mc.Amount) AS 
Total_Cost,
ROUND(qbd.Profit_Percent,2) AS Profit_Percent,
od.Part_Family_Code AS FBGM,
ROUND(SUM(((od.Unit_Price - (oh.Total_Labor_Cost + 
oh.Total_Material_Cost + mc.Amount))/ 
NULLIF(od.Unit_Price,0.00))*100),2) AS Final_Margin 

FROM dbo.Order_Header oh
LEFT OUTER JOIN dbo.Order_Detail od ON oh.Order_Header_ID = 
od.Order_Header_ID
LEFT OUTER JOIN dbo.Miscellaneous_Charge mc ON od.Order_Detail_ID = 
mc.Order_Detail_ID
LEFT OUTER JOIN dbo.Quantity_Break_Detail qbd ON mc.Order_Detail_ID = 
qbd.Order_Detail_ID

WHERE oh.Order_Number  BETWEEN  '74991' AND '75400'  AND od.Unit_Price > 
0 AND mc.Miscellaneous_Charge_Code <> 'FREIGHT'

GROUP BY 

oh.Order_Number, mc.Miscellaneous_Charge_Code, 
mc.Amount,oh.Total_Labor_Cost, oh.Total_Material_Cost, 
qbd.Profit_Percent, od.Part_Family_Code, od.Unit_Price, od.Part_Number

结果:(某些字段未填写)

Order_Number: Sale_Price: PieceRate_Labor_Cost: Houly_Labor_Cost

75024       : 1890      : 43                  : 110.49

75025       : 4938      : 105.5               : 167.51

75025       : 4938      : 281.25              : 167.51

75026       : 5000      : 251.10              : 85.00

75027       : 4352      : 142.65              : 142.00

75027       : 4352      : 68.25               : 142.00

75028       : 1568      : 98.25               : 25.62

我希望最终输出为:

Order_Number: Sale_Price: PieceRate_Labor_Cost: Houly_Labor_Cost

75024       : 1890      : 43                  : 110.49

75025       : 4938      : 386.75              : 167.51

75026       : 5000      : 251.10              : 85.00

75027       : 4352      : 210.9               : 142.00

75028       : 1568      : 98.25               : 25.62

1 个答案:

答案 0 :(得分:0)

似乎您需要sum(mc.Amount)AS PieceRate_Labor_Cost(为此,您不应在分组依据中使用mc.amount)

SELECT 
oh.Order_Number AS Order_Number,
od.Part_Number AS Job_Name,
od.Unit_Price AS Sale_Price,
sum(mc.Amount)  AS PieceRate_Labor_Cost,
oh.Total_Labor_Cost AS Hourly_Labor_Cost,
oh.Total_Material_Cost AS Total_Material_Cost,
SUM(oh.Total_Labor_Cost + oh.Total_Material_Cost+ mc.Amount) AS 
Total_Cost,
ROUND(qbd.Profit_Percent,2) AS Profit_Percent,
od.Part_Family_Code AS FBGM,
ROUND(SUM(((od.Unit_Price - (oh.Total_Labor_Cost + 
oh.Total_Material_Cost + mc.Amount))/ 
NULLIF(od.Unit_Price,0.00))*100),2) AS Final_Margin 

FROM dbo.Order_Header oh
LEFT OUTER JOIN dbo.Order_Detail od ON oh.Order_Header_ID = 
od.Order_Header_ID
LEFT OUTER JOIN dbo.Miscellaneous_Charge mc ON od.Order_Detail_ID = 
mc.Order_Detail_ID
LEFT OUTER JOIN dbo.Quantity_Break_Detail qbd ON mc.Order_Detail_ID = 
qbd.Order_Detail_ID

WHERE oh.Order_Number BETWEEN 74991 AND 75400 AND od.Unit_Price > 
0 AND mc.Miscellaneous_Charge_Code <> 'FREIGHT'

GROUP BY 

oh.Order_Number, mc.Miscellaneous_Charge_Code, 
oh.Total_Labor_Cost, oh.Total_Material_Cost, 
qbd.Profit_Percent, od.Part_Family_Code, od.Unit_Price, od.Part_Number