MYSQL 按行计数分组忽略连接表上的 JOIN 和 SUM 字段的影响

发布于 2024-12-01 07:22:32 字数 1233 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在巴黎塔顶看东京樱花 2024-12-08 07:22:32

尝试 COUNT(distinct Orders.order_id) 作为 number_of_orders,

SELECT 
  customer_id, 
  COUNT(distinct Orders.order_id) as number_of_orders, 
  SUM(Details.ordered_qty) AS total_qty_ordered, 
  (select SUM(Parcels.picked_qty) 
    FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked 
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id    
GROUP BY Orders.customer_id

编辑中所示:添加了带有子选择的其他选择

try COUNT(distinct Orders.order_id) as number_of_orders,
as in

SELECT 
  customer_id, 
  COUNT(distinct Orders.order_id) as number_of_orders, 
  SUM(Details.ordered_qty) AS total_qty_ordered, 
  (select SUM(Parcels.picked_qty) 
    FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked 
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id    
GROUP BY Orders.customer_id

EDIT: added an other select with subselect

压抑⊿情绪 2024-12-08 07:22:32

您是否有任何特殊原因认为需要将所有这些内容合并到一个查询中?通过将其分解为单独的查询来简化,如果您希望通过一次调用来获取结果,请使用临时表将查询放入存储过程中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文