需要帮助对可用订单详细信息 table 进行分组以得出 count(order_line_item) 、Qty 和 count(Qty) 的分布

Need help to group the available order details table to arrive at the distribution of count(order_line_item) , Qty and count(Qty)

我有一个订单 table 包含以下信息

订单 ID、产品 ID、订购数量

OID PID Qty
1   10  1
1   20  2
2   10  2
2   40  4
3   50  1
3   20  3
4   30  1
4   90  2
4   90  5
5   10  2
5   20  2
5   70  5
5   60  1
6   80  2

如果我运行下面查询

select `Qty`, count(`Qty`) 
from `table`
group by `Qty`

我得到 table 中的数量分布,即

Qty count(`Qty`)
1   4
2   6 
3   1
4   1
5   2

我也想求order_line_item水平的数量分布

这是有 1 个订单项、1 个数量、2 个数量等等的订单数量,例如

Count(Order_line_item)    Qty        Count(Qty)
1                         2          1
2                         1          2
2                         2          2
2                         3          1
2                         4          1
3                         1          1
3                         2          1
3                         5          1
4                         1          1
4                         2          2
4                         5          1

我应该在上面的查询中做些什么修改才能实现这个

试试这个查询

SELECT  count_order_line_items, `Qty`, count(*)
FROM (
   SELECT count(*) over (partition by `OID`) as count_order_line_items,
         `Qty`
   FROM Table1
) x
GROUP BY count_order_line_items, `Qty`

演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=07dfb27a7d434eca1f0b9641aadd53c8

如果你的 mysql 版本低于 8,那么试试这个

SELECT count_order_line_items, `Qty`, count(*)
FROM Table1 t1
JOIN (
   SELECT `OID`, count(*) as count_order_line_items
   FROM Table1
   GROUP BY `OID`
) t2 ON t1.`OID` = t2.`OID`
GROUP BY count_order_line_items, `Qty`

演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=28c291a4693f31029a103f5c41a97d77