Question

I have a stored procedure that fetches data and currently orders it as follows:

ORDER BY    dateEsc desc, prio desc, EID desc

Is there a way that I can say IF prio = "Urgent" THEN put such a record on the very top of my results and only for the remaining records apply the above order ?

Was it helpful?

Solution

Something like the following will work, because when prio = 'Urgent' then the new condition will generate 0 else 1 and sort by this value first; and later sort by other conditions...

ORDER BY CASE WHEN prio = "Urgent" THEN 0 ELSE 1 END,
    dateEsc desc, prio desc, EID desc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top