user892134
user892134

Reputation: 3214

mysql query order by category top sales

Having trouble with how to do this query. There are three categories, ship,car, plane. I want to order category by most sales.

Here is the SQL fiddle http://sqlfiddle.com/#!2/e3344/1

Here is the table values

 id  |  name        |  sales  | category  |
   1 |  mike        |    2    |    ship   |
   2 |  john        |    11   |    car    |
   3 |  david       |    13   |    ship   |
   4 |  pablo       |    24   |    car    |
   5 |  greg        |    13   |    car    |
   6 |  nick        |    1    |    ship   |
   7 |  anderson    |    19   |    ship   |
   8 |  matt        |    10   |    plane  |
   9 |  robbie      |    3    |    ship   |
  10 |  victor      |    1    |    ship   |
  11 |  ben         |    11   |    plane  |
  12 |  rick        |    6    |    ship   |
  13 |  christopher |    16   |    car    |
  14 |  steve       |    8    |    ship   |
  15 |  claudio     |    9    |   plane   |

How do i add up total sales by category and order DESC?

Upvotes: 0

Views: 440

Answers (1)

timbmg
timbmg

Reputation: 3338

Try the group by and order by statement.

SELECT category, sum(sales)
FROM table
GROUP BY category
ORDER BY sum(sales) DESC

Edit Just as a suggestion. Your "category" should be an extra entity in your database model.

Upvotes: 2

Related Questions