如何将一个字段的总和与另一个字段的总和进行比较?
How can I compare the sum of one field to another single field?
我的数据集是这样的:
- Total_Order_Table
- Order_no(唯一)
- Shipped_quantity
- Order_Detail_Table
- Order_number(不唯一)
- Quantity_per_bundle
我需要从 Order_Detail_Table 中为每个 order_number 取 Quantity_per_bundle 的总和,并将其与 Shipped_quantity 进行比较。
我的想法是外连接,这样我的数据看起来像这样:
我需要能够看到数量差异以及订单号是否存在于两个表中。
提前致谢!
除非我在问题中遗漏了一些微妙的东西,否则这不是像 SELECT
查询一样简单,在表之间进行连接。
类似这样的事情应该会达到结果:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(代码未在 MySQL 中测试,所以请原谅错误)
通常在 sql 中使用 FULL OUTER JOIN:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
但是 mysql 中不存在 FULL OUTER JOIN。但是你可以模拟它:http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
我的数据集是这样的:
- Total_Order_Table
- Order_no(唯一)
- Shipped_quantity
- Order_Detail_Table
- Order_number(不唯一)
- Quantity_per_bundle
我需要从 Order_Detail_Table 中为每个 order_number 取 Quantity_per_bundle 的总和,并将其与 Shipped_quantity 进行比较。
我的想法是外连接,这样我的数据看起来像这样:
我需要能够看到数量差异以及订单号是否存在于两个表中。
提前致谢!
除非我在问题中遗漏了一些微妙的东西,否则这不是像 SELECT
查询一样简单,在表之间进行连接。
类似这样的事情应该会达到结果:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(代码未在 MySQL 中测试,所以请原谅错误)
通常在 sql 中使用 FULL OUTER JOIN:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
但是 mysql 中不存在 FULL OUTER JOIN。但是你可以模拟它:http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/