Solved

Sorting a union select statement?

Posted on 2011-09-07
11
212 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 47
Benefits of SMB Fileshare 3 63
Unable to save view in SSMS 21 59
SQL Query Syntax Join 4 34
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

16 Experts available now in Live!

Get 1:1 Help Now