SQL multi-Conditional ordering

Source: Internet
Author: User

A multi-conditional sort can be implemented by using a case and then conditional statement after the order by statement.

Select *  from ORDER  by  Case  when  Then 0 Else 1 End

Example:

1. Create a table Case_test

There are id,case_type,case_location,case_way of four fields.

2. Import data:
INSERT  into"Xiangzh". " Case_test "VALUES('1','theft Case','Taidong','Technology Unlock');INSERT  into"Xiangzh". " Case_test "VALUES(' the','murder Case','Taidong','Technology Unlock');INSERT  into"Xiangzh". " Case_test "VALUES(' About','theft Case','Jiangxi Road','Technology Unlock');INSERT  into"Xiangzh". " Case_test "VALUES('5','theft Case','Taidong','Violent Unlocking');INSERT  into"Xiangzh". " Case_test "VALUES('6','theft Case','Jiangxi Road','Violent Unlocking');INSERT  into"Xiangzh". " Case_test "VALUES('7','murder Case','Taidong','Violent Unlocking');INSERT  into"Xiangzh". " Case_test "VALUES('8','murder Case','Jiangxi Road','Technology Unlock');INSERT  into"Xiangzh". " Case_test "VALUES('9','murder Case','Jiangxi Road','Violent Unlocking');INSERT  into"Xiangzh". " Case_test "VALUES('Ten','theft Case','Taidong','Technology Unlock');

Not sorted:

3. Multi-conditional grouping sorting
Select *  from"Case_test"ORDER  by  Case  when"Case_type"='theft Case'  Then 0 Else 1 End, Case  when"Case_location"= 'Taidong'  Then 0 Else 1 End, Case  when"Case_way"= 'Technology Unlock'  Then 0 Else 1 End ASC

The query results are sorted by conditional grouping:

4. Sort the number of conditions
Select *  from"Case_test"ORDER  by  Case  when"Case_type"='theft Case'  and"Case_location"= 'Taidong'  and"Case_way"= 'Technology Unlock'  Then 0 when"Case_type"='theft Case'  and"Case_location"= 'Taidong'  Then 1 when"Case_type"='theft Case'  and"Case_way"= 'Technology Unlock'  Then 2 when"Case_location"= 'Taidong'  and"Case_way"= 'Technology Unlock'  Then 3 when"Case_type"='theft Case'  Then 4 when"Case_location"= 'Taidong'  Then 5 when"Case_way"= 'Technology Unlock'  Then 6Else 7End

The query results are sorted by the number of criteria that are met:

If there is a more reasonable SQL writing, please leave a message to discuss.

Reference:

Examples of Oracle conditionally ordered

How Oracle Sorts by criteria

SQL multi-Conditional ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.