Link to home
Start Free TrialLog in
Avatar of jmoran18251
jmoran18251

asked on

Group By and Order By

I am fairly new to SQL and seem to be stuck... I would greatly appreciate any help.  I am trying to write an SQL statement that will tell me what text appears most often in my data.  For example, if I have

Ice Cream
Cookie
Cake
Ice Cream
Muffin

I would like to get 'Ice Cream'

Here's what I have come up with...

"SELECT CC as Complaint, COUNT(CC) AS Total from Data WHERE XYZ GROUP BY CC", which returns

Cake             1
Cookie          1
Ice Cream     2
Muffin           1

I was thinking if I could at least order the 'Total' column I could just always grab the top result, but I cannot seem to get the 'Order By' function to work with the field.  Any idea how to make this work or another way to get the result I am looking for?  Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with mysql, to get only the "top winner":

SELECT CC as Complaint, COUNT(*) AS Total from Data WHERE XYZ GROUP BY CC ORDER BY COUNT(*) DESC LIMIT 1 "

Open in new window

Avatar of jmoran18251
jmoran18251

ASKER

I figured I had to be in the ballpark, but I just couldn't quite get there.  Thank you!  That was killing me!