Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Two select statements

Author  Topic 

haoli12345
Starting Member

2 Posts

Posted - 2007-09-13 : 11:46:39
I have a table that list Canadian provinces and American States it looks something like this:

ID | ProvState

Under ID 1-13 lists the Canadian provinces and everything over 13 lists the American states. I want to create 1 query that will list the Canadian provinces first in alphabetical order then the States in alphabetical order.

I have tried using UNION but it's not returning what I want and it does not allow me to use order by for the first statement.

SELECT * FROM SPProvince WHERE ID < 14 ORDER BY ProvState
UNION
SELECT * FROM SPProvince WHERE ID > 13 ORDER BY ProvState

Anyone have any suggestions to this problem?

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2007-09-13 : 11:51:38
Hi,

Try this

SELECT * FROM SPProvince
ORDER BY CASE WHEN ID < 14 THEN 1 ELSE 2 END, ProvState
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-13 : 11:52:22
[code]SELECT *
FROM SPProvince
ORDER BY CASE
WHEN ID < 14 THEN 0
ELSE 1
END,
ProvState[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

haoli12345
Starting Member

2 Posts

Posted - 2007-09-13 : 13:45:12
WOW you guys are awesome thanks.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-09-13 : 14:38:01
quote:
Originally posted by haoli12345

WOW you guys are awesome thanks.



What were you expecting?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -