views:

63

answers:

2

Hi,

I have a my sql table with customer names and orders that they placed over time.

table column names are id, customer name, order value, status, and created and modified.

I am able to group customer names and number of orders.

using a sql query like this..

SELECT Customer,count(OrderPrice) FROM Orders GROUP BY Customer

It works fine. But I want the output to be ordered by number of orders(count). Customer with lots of orders comes at the top of the list.

I appreciate any help.

Thanks.

+4  A: 
    SELECT Customer
         , count(OrderPrice) cnt
      FROM Orders
  GROUP BY Customer
  ORDER BY cnt DESC
dcp
FYI: The number in the ORDER BY is called an "ordinal" - it refers to the column value in the 2nd position of the SELECT clause in this example. Not a recommended practice - if the column order changes, the ordering will still be based on whatever the second column is.
OMG Ponies
@OMG Ponies - Fair enough, I fixed it :).
dcp
I already upvoted
OMG Ponies
@OMG Ponies - Thanks, I appreciate it :).
dcp
+1  A: 

Hi,

SELECT Customer,
       count(OrderPrice) order_count 
FROM Orders 
GROUP BY Customer
ORDER BY order_count desc 

Enjoy!

Doug
@Doug - Your GROUP BY / ORDER BY are backwards (i.e. GROUP BY comes before ORDER BY).
dcp
Good catch - Thanks!
Doug