我有一个如下所示的 SQL 表:
item_price | discount | quantity
2000.00 | 360.00 | 02
2000.00 | 360.00 | 01
1589.00 | 250.00 | 01
我如何获得每个 item_prices 的所有出现但排除折扣(包括多个数量的折扣)?例如:对于 item_price '2000.00',我会得到它出现的 3。
我有:
select count(item_price) * quantity as occurrence from order_items group by 1;
但它只为 item_price '2000.00' 返回 2
最佳答案
按 item_price 分组,按 item_price 和 (qty*item_price) 的值以及平均折扣计数:
select
item_price
, count(*) as occurrence
, sum(item_price * quantity) as value_of
, avg(discount) as avg_discount
from order_items
group by
item_price
;
关于mysql - 如何获取每个不同值的所有出现,包括它在 mysql 中的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47481568/