Solved

Sorting a union select statement?

Posted on 2011-09-07
11
210 Views
Last Modified: 2012-06-27
Hello experts.  Hoping for some help. My Statement below isn't working.

SELECT DISTINCT State
FROM         cities
WHERE     (Country = 'Canada')
Order By State
UNION
SELECT DISTINCT State
FROM         cities
WHERE     (Country = 'us')
Order by State

What I am trying to do is have all of the Canadian provinces listed FIRST, alphabetized, followed by all the US states, alphabetized.  Maybe Union is the wrong way to go?

0
Comment
Question by:arthurh88
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 36499206
Try this:

SELECT DISTINCT state
FROM cities
WHERE (Country = 'Canada') OR (Country = 'us')
ORDER BY Country, State
0
 
LVL 18

Expert Comment

by:lludden
ID: 36499302
Or
SELECT DISTINCT state
FROM cities
WHERE Country IN ('Canada','us')
ORDER BY Country, State

I like to use IN rather than OR in case you add other conditions you don't have to worry about grouping conditions.
0
 

Author Comment

by:arthurh88
ID: 36499304
no go.  I got this error:  ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36499363
@AriMc - I don't think your order by will work because Country is not in the selection

Try this:

select State from
(
  select distinct State, Country
  from cities
  where Country in ('Canada','us')
  order by Country, State
)
0
 
LVL 9

Expert Comment

by:AriMc
ID: 36499441
@gerwinjansen - That probably depends on the DB-engine. Your query seems to give errors regarding unambiguous aliases. This one works at least in MySQL:

select ns.State from
(
  select distinct State, Country
  from cities
  where Country in ('Canada','us')
  order by Country, State
)  ns

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36499480
@AriMc - OK :)

@arthurh88 - last one working for you?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36499488
You should use a CASE statement in the ORDER BY.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36499511
Try like this:

SELECT DISTINCT [State]
FROM cities
ORDER BY CASE [Country] WHEN 'Canada' THEN 0 ELSE 1 END, [State]

Open in new window


And you may want to look at GROUP BY as you may want the [Country] involved in case you are using the abbreviations for states as I believe there is at least AL that conflicts.

SELECT [State]
FROM cities
-- WHERE [Country] IN ('Canada', 'US') 
GROUP BY [Country], [State]
ORDER BY CASE [Country] WHEN 'Canada' THEN 0 ELSE 1 END, [State]

Open in new window


Uncomment the WHERE clause if you have other countries in your cities table.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36499518
Oh, and first query would work also if you use GROUP BY. I over "thunk" it since 'Canada' alphabetically sorts before 'US' anyway, you don't really need the CASE.

Change http:#a36499206 to:
SELECT [State]
FROM cities
WHERE [Country] IN ('Canada', 'US') 
GROUP BY [Country], [State]
ORDER BY [Country], [State]
;

Open in new window


Hope that helps!
0
 

Author Closing Comment

by:arthurh88
ID: 36499709
yes this one worked!

I tried

SELECT     State
FROM         (SELECT DISTINCT TOP (100) PERCENT State, Country
                       FROM          cities
                       WHERE      (Country IN ('Canada', 'us'))
                       ORDER BY Country, State) AS derivedtbl_1

but it did not work, it mixed US and Canadian states together.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36499775
Yes, sort in a view which the derived table is acting like doesn't really guarantee sorting on the outer query. It is the reason why sorting views is useless. Glad that helped.

Best regards and happy coding,
Kevin
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore 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.
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.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

11 Experts available now in Live!

Get 1:1 Help Now