我想计算 2 个组的“数量”贡献百分比,其中“客户 ID”出现多次的组 1 和“客户 ID”出现一次的组 2。例如:
|ID | OrderID |Quantity
|---|-------- |-----
|1 |10248 |12
|2 |10248 |10
|3 |10248 |5
|4 |10249 |9
|5 |10249 |40
|6 |10250 |10
-----进入-----
OrderID |Quantity
10248 |27
10249 |49
10250 |10
-----最终进入-----
Group % tot Quantity
1 12% <--- Just OrderID 10250 since it has only 1 order
2 88%
最佳答案
select
100.0 * sum(case when cnt = 1 then sumQty else 0 end) / sum(sumQty) as "single Order"
,100.0 * sum(case when cnt <> 1 then sumQty else 0 end) / sum(sumQty) as "multiple Orders"
from
(
select
OrderID
,sum(Quantity) as sumQty
,count(*) as cnt
from myTable
group by OrderId
) as dt
关于mysql - 根据一定条件SQL计算各组贡献百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580961/