简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM