?
Solved

Sorting a union select statement?

Posted on 2011-09-07
11
Medium Priority
?
217 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36499480
@AriMc - OK :)

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

Expert Comment

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

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 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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