Solved

SQL Query Help

Posted on 2011-02-16
8
351 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
[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
8 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34911112
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
ID: 34911121
is this what you are looking for

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

Author Comment

by:Stanton_Roux
ID: 34911351
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 26

Expert Comment

by:tigin44
ID: 34911385
is there any order rule for listing the regions...
0
 

Author Comment

by:Stanton_Roux
ID: 34911604
no it must be displayed exaclty as it is in the second table
0
 
LVL 39

Accepted Solution

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

Expert Comment

by:tigin44
ID: 34911720
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
ID: 34911756
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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