Oh yeah. Most implementations require that the columns in the ORDER BY clause be present in the SELECT list for a SELECT DISTINCT. It's because this is actually a grouping operation. Your particular SQL implementation may also require it. (I think the argument goes that you shouldn't present something in an order that cannot be verified. If an ORDER BY value is not in the SELECT list, you can't tell if the order is correct...)
Anyway, you can just add the CASE expression to your SELECT list, name it as an alias, and ORDER BY that:
Code:
SELECT DISTINCT osirHazards.id, .... ,
CASE WHEN osirHazards.status = 0 THEN 9999 ELSE osirHazards.status END AS Sortorder
FROM ...
ORDER BY Sortorder
(why are you using a SELECT DISTINCT in this query - can there really be duplicates?)
Uh-oh - I see that you want to present this data in a grid on a page so the user can click on this status column to sort by it. This may be trickier than it looks - to get the underlying column to sort correctly yet still display it's actual value.
Your best (and certainly simplest) solution may have already been posted - change the status values so they naturally sort the way you want.
Jeff Mason
[email protected]