starsinmypockets
starsinmypockets

Reputation: 2294

Return descending list of products by total sold from line item table in mysql

I have a mysql table:

line_item_id   order_id  product_id  qty

     1              1         144     4
     2              1         577     1
     3              1         474     1
     4              2         909     10
     5              2         474     1

How can I sort this table so that it returns the product_id's in a list of descending order by the total number sold (eg 909, 144, 474, 577)?

Upvotes: 0

Views: 70

Answers (1)

Derek
Derek

Reputation: 23248

select product_id, sum(qty) as qty_sum
from table
group by product_id
order by qty_sum desc

Upvotes: 3

Related Questions