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.
| Author |
Topic |
|
adiki
Starting Member
1 Post |
Posted - 2004-04-14 : 01:15:22
|
| Hi, I need to wirte a query for obtaining the following outputcountry_id country_name----------- -------------120 USA119 UK101 ARGENTINA102 ALABAMA103 BANGLADESH104 BRUNEI105 CANADA---- AND GOES ON IN ASCENDING ORDERThe first two rows will always be USA and UK and the rest of countries should be in ascending order.any solution!!!RegardsSrini |
|
|
gpl
Posting Yak Master
195 Posts |
Posted - 2004-04-14 : 06:21:47
|
| Add a new BIT column PriorityCountry, make it 1 for UK and USA, 0 for all others then sort on PriorityCountry DESC and country_name ASCThis has the 'benefit' of putting UK first too <vbg/>Graham |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-04-14 : 06:22:46
|
| investigate (ie search here) ORDER BY + CASE....you'll find some examples that will teach you how to solve this problem.... |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2004-04-14 : 06:51:23
|
Hey! Whattabout Norway?? |
 |
|
|
kroky
Starting Member
14 Posts |
Posted - 2004-04-14 : 07:09:23
|
| Hey! Whattabout Norway??this post rox.... |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2004-04-14 : 09:29:18
|
quote: Originally posted by gpl Add a new BIT column PriorityCountry, make it 1 for UK and USA, 0 for all others then sort on PriorityCountry DESC and country_name ASCThis has the 'benefit' of putting UK first too <vbg/>Graham
or make PriorityCountry 2 for USA and 1 for UK and 0 for all else, then sort descending... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-04-14 : 09:33:34
|
| [code]USE NorthwindGOCREATE TABLE myTable99(country_id int, country_name varchar(255))GO INSERT INTO myTable99(country_id, country_name)SELECT 120, 'USA' UNION ALLSELECT 119, 'UK' UNION ALLSELECT 101, 'ARGENTINA' UNION ALLSELECT 102, 'ALABAMA' UNION ALLSELECT 103, 'BANGLADESH' UNION ALLSELECT 104, 'BRUNEI' UNION ALLSELECT 105, 'CANADA' UNION ALLSELECT 106, 'NORWAY' SELECT * FROM myTable99ORDER BY CASE WHEN Country_Name = 'USA' THEN 1 WHEN Country_Name = 'UK' THEN 2 ELSE 3 END , Country_NameGODROP TABLE myTable99GO[/code]Brett8-) |
 |
|
|
|
|
|
|
|