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 2008 Forums
 Transact-SQL (2008)
 order by certain numbers

Author  Topic 

grimmus
Yak Posting Veteran

53 Posts

Posted - 2012-01-10 : 04:20:23
Hi,

I have a resultset that contains between 3 and 10 rows. Each result has an ID and i need to order the rows in the following way

ID
7
1
8
then the others here, if relevant

Is there a way that I can explicitly say that i want rows with ID 7,1,8 to appear at the top of the resultset ? There is no other column that could help with the ordering at this point.

Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 04:25:10
you can do something like

..
ORDER BY CASE Yourfield WHEN 7 THEN 0 WHEN 1 THEN 1 WHEN 8 THEN 2 ELSE 3 END,Yourfield


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

grimmus
Yak Posting Veteran

53 Posts

Posted - 2012-01-10 : 04:58:04
excellent , thank you. Ill give it a shot.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 06:43:44
welcome...let us know how you got on

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -