简体   繁体   English

在sql查询中自定义顺序

[英]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. 我有5个类别,我用于order by子句。 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. 我已经尝试过asc和desc的命令也搜索了一些论坛,但它没有清除我的概念。 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 . 文档在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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