Solved

Group By and Order By

Posted on 2008-06-16
3
183 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

11 Experts available now in Live!

Get 1:1 Help Now