0

好的,我有这种结构的表:

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'sorder_id's每组的总价格中计算出相同的价格product_ids

4

3 回答 3

2

要获得明智的产品COUNTSUM您需要使用聚合函数 GROUP BYproduct_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 
于 2012-08-20T06:06:57.163 回答
2

尝试

SELECT PRODUCT_ID,
       COUNT(product_id) totalCount,
       SUM(price) totalPrice
FROM tableName
WHERE Order_ID BETWEEN 00001  AND 99999
GROUP BY PRODUCT_ID
于 2012-08-20T06:14:56.173 回答
0

尝试这个:

您只需按 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
于 2012-08-20T06:07:30.743 回答