Sql个人和

Sql个人和,sql,Sql,我目前正在从各个表收集数据。我应该得到每批货的总金额*重量。然而,它给了我总的总和*权重,这意味着我得到了相同的结果 以下是我执行的代码: SELECT SUM(Weight*Amount), Arrival_Date FROM Specifications, Shipment, `Product shipment` WHERE Specifications.Product_Code = `Product shipment`.Product_Code AND `Product shipme

我目前正在从各个表收集数据。我应该得到每批货的总金额*重量。然而,它给了我总的总和*权重,这意味着我得到了相同的结果

以下是我执行的代码:

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

我错过了什么

您应该按装运ID分组,而不是按到达日期分组。 我不确定,但可能需要在SELECT子句中显示装运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 `Product shipment`.Shipment_ID, -- <- try add this
         Arrival_Date

您没有按装运ID分组以获取每个装运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

这不应该是按发货编号分组吗?按产品发货编号分组,然后按日期分组不是更公平吗?@bestprogrammerintheworld:你说得很对-你的版本更容易阅读。我编辑了这篇文章。我如何避免结果像那样重复?@Avacay:你应该检查条件注释出SUM和GROUP BY的位置,检查返回的数据以及GROUP BY字段,也许你必须添加类似Specifications.Product_Code的内容选择SUMWeight*数量规格、装运、装运的总重量,产品装运,其中Specification.Product_Code=产品装运。Product_Code和Product Shipping.Shipping_ID=产品装运。装运ID按装运分组。到达日期,产品装运。Product_Code此处的以下代码似乎正确!但是,正确的结果仍在重复4次!:s