Solved

Group By and Order By

Posted on 2008-06-16
3
184 Views
Last Modified: 2010-04-21
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!
0
Comment
Question by:jmoran18251
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21791880
you where close:
SELECT CC as Complaint, COUNT(*) AS Total from Data WHERE XYZ GROUP BY CC ORDER BY COUNT(*) DESC "

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21791882
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

0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now