Solved

SQL Group By

Posted on 2011-09-21
3
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
While in ##Table - Help 4 22
Negative isnull? 3 25
Parse this column 6 34
query output (script) from a stored procedure 4 36
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

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