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
 ORDER BY question

Author  Topic 

smcallister
Starting Member

15 Posts

Posted - 2009-06-05 : 15:00:56
I have 12 rows in table with a column called "type". There are 4 rows with type "1", 4 rows with type "2", and 4 rows with type "3".

Depending on which PHP page I'm on, I want to be able to pull up all type "X" and then below that list the rest of the rows ordered by type.

So for instance, this would appear on "items.php?type=2":

Dog Type 2
Cat Type 2
Frog Type 2
Fish Type 2

Car Type 1
Plane Type 1
Train Type 1
Bike Type 1

House Type 3
Condo Type 3
Apt Type 3
Hotel Type 3

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-05 : 15:15:15
try this:
order by CASE WHEN type = @type then -1 else type end

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-07 : 03:12:40
order by NULLIF(type,@type)
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-07 : 08:05:40
quote:
Originally posted by visakh16

order by NULLIF(type,@type)


This won't satisfy the requirement where the all rows with the type value of @type appears first in the list then all the others in natural order.

Your solution would fit solve a different problem where @type was a nullable input to sort by.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -