MYSQL 按行计数分组忽略连接表上的 JOIN 和 SUM 字段的影响
我有 3 个表:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
订单有多个详细信息行,每个产品一个详细信息行。
一个明细行有多个包裹,由于 10,000 个订购数量可能来自 6 个不同批次,因此批次中的货物会分开包装和发货。详细信息行中每个地块中的提货数量应与ordered_qty 相同。
...希望这是有道理的。
我正在努力编写一个查询来提供所有这些的摘要信息。
我需要按 customer_id 分组来为每个客户提供一行数据。
该行应该包含
- 他们的订单总数
- 他们所有订单中商品的总ordered_qty
- 他们所有订单中商品的总pick_qty
我可以得到第一个:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
但是当我离开连接其他两个表并添加
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
..然后我得到结果似乎没有增加数量,并且 COUNT(*) 似乎包括来自 JOIN 的附加行,这显然不再给我订单数。
不确定下一步要尝试什么。 ===== 编辑=======
这是我尝试过的查询:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
I have 3 tables:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
Orders have multiple Details rows, a detail row per product.
A detail row has multiple parcels, as 10'000 ordered qty may come from 6 different batches, so goods from batches are packed and shipped separately. The picked quantity put in each parcel for a detail row should then be the same as the ordered_qty.
... hope that makes sense.
Im struggling to write a query to provide summary information of all of this.
I need to Group By customer_id to provide a row of data per customer.
That row should contain
- Their total number of orders
- Their total ordered_qty of goods across all orders
- Their total picked_qty of goods across all orders
I can get the first one with:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
But when I LEFT JOIN the other two tables and add the
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
.. then I get results that dont seem to add up for the quantities, and the COUNT(*) seems to include the additional lines from the JOIN which obviously then isn't giving me the number of Orders anymore.
Not sure what to try next.
===== EDIT =======
Here's the query I tried:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
COUNT(distinct Orders.order_id) 作为 number_of_orders,
如
编辑中所示:添加了带有子选择的其他选择
try
COUNT(distinct Orders.order_id) as number_of_orders,
as in
EDIT: added an other select with subselect
您是否有任何特殊原因认为需要将所有这些内容合并到一个查询中?通过将其分解为单独的查询来简化,如果您希望通过一次调用来获取结果,请使用临时表将查询放入存储过程中。
Is there any particular reason you feel the need to combine all these in one query? Simplify by breaking it up in to separate queries, and if you want a single call to get the results, put the queries in a stored procedure, using temp tables.