user1749244
user1749244

Reputation:

Custom order by in sql query

I am working a report which require custom order by statement. I have 5 categories which I am using for the order by clause. These are my column names student expenses, petty cash expenses, home expenses, daily expenses, Fair expenses. I want to order them in report in this manner:

Pettycash
DailyExpense
Home Expense
Fair Expense
Cash Expense

Also if new categories are added that wont effect this order and added in the last. I have tried order by asc and desc also searched some forums but it didnt cleared my concept. Thank you.

Upvotes: 3

Views: 157

Answers (1)

Michal Klouda
Michal Klouda

Reputation: 14521

Try following:

ORDER BY
CASE Category
   WHEN 'Pettycash' THEN '1'
   WHEN 'DailyExpense' THEN '2'
   WHEN 'Home Expense' THEN '3'
   WHEN 'Fair Expense' THEN '4'
   WHEN 'Cash Expense' THEN '5'
   ELSE '6' 
END

Documentation found here.

Upvotes: 2

Related Questions