Solved

SQL Group By

Posted on 2011-09-21
3
190 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
ID: 36576710
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
ID: 36581865
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
ID: 36583286
Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

21 Experts available now in Live!

Get 1:1 Help Now