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 2005 Forums
 Transact-SQL (2005)
 Dynamic ORDER BY

Author  Topic 

monfu
Yak Posting Veteran

81 Posts

Posted - 2008-04-29 : 05:40:22
Dear All,

I have a set of rows, a sample listed below

ID Status PageID
1 1 0
2 1 12
3 2 15
4 4 0
5 0 0
6 4 13
7 0 0
8 4 15
9 2 0

Now I wish to list the rows in Status ASC order, however, only those rows that have the Page ID > 0 will be ordered.

How can i achieve that?

I tried something like
ORDER BY CASE pageID > 0 THEN Status ASC

However I have a syntax error

Thanks for your help and time

Johann

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-29 : 05:53:14
ORDER BY CASE WHEN pageID > 0 THEN Status ASC




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

monfu
Yak Posting Veteran

81 Posts

Posted - 2008-04-29 : 05:59:44
did not work Peso

Incorrect syntax near the keyword 'ASC'.

Anyway clarified with my project leader this should be fine
ORDER BY PageID DESC, Status ASC

thanks for all you help
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-29 : 06:16:30
ORDER BY CASE WHEN pageID > 0 THEN Status END ASC


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

- Advertisement -