I have requirements for a custom ORDER BY clause that looks much like this
SELECT * FROM example
ORDER BY
CASE
WHEN name = 'I want this first' THEN 0
WHEN name = 'I want this second' THEN 1
WHEN name = 'We get the picture' THEN 2
ELSE 99 END ASC
However, this case statement has grown and I want to be able to reuse the sort order for other queries.
I see my options being
- Copy Paste!
- Adding another column to the table specifically for this sort order
In my head, it seems like I should be able to pass a function to perform the sorting logic. But after some searching on SO and google I couldn't find anything and thought this might also help someone else down the road.