Solved

SQL Query Help

Posted on 2011-02-16
8
346 Views
Last Modified: 2012-08-13
Hi There

I need a SQL query that will display the following table

Region      Contact
Cape Town      Fred
CapeTown      Joe
Cape Town      Anna
Durban      John
Durban      Mary
Johannesburg      Frank

and display the results like this

Region      Contact
Durban      John
Durban      Mary
Johannesburg      Frank
Cape Town      Anna
CapeTown      Fred
Cape Town      Joe

Thanks
Stanton
0
Comment
Question by:Stanton_Roux
8 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
this would be the first one
select  region, contact from TABLE order by region asc, contact desc
is there any pattern in the second one ?
0
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
is this what you are looking for

SELECT Region,      Contact
FROM yourtable
ORDER BY Region   ASC,   Contact ASC
0
 

Author Comment

by:Stanton_Roux
Comment Utility
Sorry confusing question the first table is the way the data is stored in the DB.

I want that data to be displayed like the results in the second table.
In the second table the people are in DESC order but the regions are mixed up.

So I am looking for a query to display the data the way it is in the second table
0
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
is there any order rule for listing the regions...
0
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.

 

Author Comment

by:Stanton_Roux
Comment Utility
no it must be displayed exaclty as it is in the second table
0
 
LVL 38

Accepted Solution

by:
Aaron Tomosky earned 500 total points
Comment Utility
You could order by the second letter of the region desc. But this is getting really weird.
0
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
does such a listing can help you?
SELECT Y.Region,  Y.Contact
FROM yourtable Y
INNER JOIN (SELECT Region, ROW_NUMBER() OVER (ORDER BY Contact) AS regionOrder
			FROM (SELECT Region, MIN(Contact) AS Contact
				  FROM yourTable) A) X ON Y.Region = X.Region
ORDER BY X.regionOrder DESC,   Y.Contact ASC

Open in new window

0
 

Author Comment

by:Stanton_Roux
Comment Utility
Thanks Aaron

Heres the solution

SELECT Region,      Contact
FROM Test
order by Substring(LEFT(Region,2),2,Len(Region)-3) DESC ,Contact ASC
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Creating and Managing Databases with phpMyAdmin in cPanel.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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