Sql个人SUM

时间:2014-05-07 09:55:40

标签: sql

我目前正在从各个表中收集数据。我应该得到我的总和(金额*重量)每次发货。然而它给了我整体总和*重量,这意味着我一直得到相同的结果。

以下是我执行的代码:

SELECT SUM(Weight*Amount), Arrival_Date 
FROM Specifications, Shipment, `Product shipment` 
WHERE Specifications.Product_Code = `Product shipment`.Product_Code 
AND `Product shipment`.Shipment_ID = `product Shipment`.shipment_ID 
GROUP BY Arrival_Date

我错过了什么?

3 个答案:

答案 0 :(得分:1)

扩展您的查询的GROUP BY

  SELECT SUM(Weight * Amount), 
         Arrival_Date 
    FROM Specifications, 
         Shipment, 
        `Product shipment` 
   WHERE Specifications.Product_Code = `Product shipment`.Product_Code 
     AND `Product shipment`.Shipment_ID = `product Shipment`.shipment_ID 
GROUP BY `Product shipment`.Shipment_ID, -- <- try add this
         Arrival_Date

答案 1 :(得分:0)

您应按装运_ID分组,而不是Arrival_Date。 我不确定,但可能需要在SELECT子句中出现shipment_ID才能使其正常工作。

答案 2 :(得分:0)

您未按装运单ID分组,以获取每个Shipment_ID的总和

SELECT SUM(Weight*Amount), Arrival_Date 
  FROM Specifications, Shipment, `Product shipment` 
WHERE Specifications.Product_Code = `Product shipment`.Product_Code 
  AND `Product shipment`.Shipment_ID = `product Shipment`.shipment_ID 
  GROUP BY Shipment`.shipment_ID ,Arrival_Date