Solved

SQL Group By

Posted on 2011-09-21
3
189 Views
Last Modified: 2012-05-12
I have a sql statement which includes a field for the 'city' and I Group By the city.  It is possible to specify which city I want at the top of the list and then have the rest of the cities sorted below it?  Example:
101 E LEMON, TARPON SPRINGS
102 E LEMON, TARPON SPRINGS
101 E LEMON, CLEARWATER
102 E LEMON, CLEARWATER
101 E LMEON, ST PETERSBURG
As always, thanks!
0
Comment
Question by:MEINMEL
3 Comments
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 total points
Comment Utility
You could do something like this...

ORDER BY
   CASE WHEN City = 'Tarpon Springs' THEN 1 ELSE 2 END,
   City
0
 
LVL 1

Expert Comment

by:millsap_singer
Comment Utility
Tim_cs's answer will work fine, but if you have a table for cities then it would be best to put the preferred sort order there.  

But if you want to sort the cities with the most addresses then you can put the ORDER BY COUNT(*) DESC at the end of your query.  Or whatever your aggregate function is.  

0
 

Author Closing Comment

by:MEINMEL
Comment Utility
Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

763 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

6 Experts available now in Live!

Get 1:1 Help Now