Solved

Sorting a union select statement?

Posted on 2011-09-07
11
215 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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.

730 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