好的,我有这种结构的表:
order_id
product_id
price
18645
289
5.90
18644
219
1.80
18644
109
2.30
18643
289
4.50
18642
180
9.80
18642
190
3.40
我如何能够product_id's
从order_id's
每组的总价格中计算出相同的价格product_ids
?
要获得明智的产品COUNT
,SUM
您需要使用聚合函数 GROUP BY
product_id:
SELECT product_id,
COUNT(1) AS product_count,
SUM(price) AS total_price
FROM table_name
WHERE order_id BETWEEN 18642 AND 18645
GROUP BY product_id
ORDER BY product_count ASC, total_price ASC
尝试
SELECT PRODUCT_ID,
COUNT(product_id) totalCount,
SUM(price) totalPrice
FROM tableName
WHERE Order_ID BETWEEN 00001 AND 99999
GROUP BY PRODUCT_ID
尝试这个:
您只需按 product_id 分组
select product_id,
count(*) as count ,
sum(price) as price
from <table>
where order_id beteen <val1> and <val2>
group by product_id