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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 query using order by

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-04-21 : 10:20:18
Srinivas writes "Hi ,

Can we write a query to do the following.

Country_id Country_name
----------- --------------
120 USA
118 UK
100 Argentina
102 Alabama
103 Bangladesh
104 Canada


and so on

It is required that USA and UK are always on the top
and the rest of the countries are ordered by
country name in ascending order

Any Pointers?

REgards
srinivas"

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-04-21 : 10:28:29
CREATE TABLE #Country(CountryID INT, CountryDesc VARCHAR(200))
INSERT INTO #Country VALUES(120,'USA')
INSERT INTO #Country VALUES(118, 'UK')
INSERT INTO #Country VALUES(100, 'Argentina')
INSERT INTO #Country VALUES(102, 'Alabama')
INSERT INTO #Country VALUES(103, 'Bangladesh')
INSERT INTO #Country VALUES(104, 'Canada')

select CountryID, CountryDesc, CASE WHEN CountryID = 120 then 0 WHEN CountryID = 118 THEN 1 ELSE 2 END as sid
FROM #Country
ORDER BY 3, 2


Duane.
Go to Top of Page
   

- Advertisement -