"SQL" is sorted by specific required elements

Source: Internet
Author: User

In Oracle, we sort the usual order by column names in order, but sometimes we want some of the elements in this column to be in front or in the back or somewhere in the middle.

At this point we can use the order by case and then method.

For example: We want the leadership of this department to be in front of the name.

Select *  fromParterwhereLvl='leader' Order  by( Case  whenNameinch('Zhangsan','Lisi','Wangermazi') Then 0 Else 1 End)

The effect is: ' Zhangsan ', ' Lisi ', ' Wangermazi ' are sorted from beginning to end, and the remaining elements are randomly sorted after the three elements. Replace 1 and 0, and turn into ' Wangermazi ', ' Lisi ', ' Zhangsan ' in reverse order.

0 is the position of the element we need, and 1 is the position of the other element.

The same effect can also be achieved through the "decode" function

Select *  fromParterwhereLvl= 'leader' Order  byDecode (name,'Zhangsan',0,'Lisi',1,'Wangermazi',2,3)

The effect is the same as above, where 0,1,2 is the position of the three elements we need, and 3 is the location of the other elements.

But the first SQL statement has a problem, if the Name field is the only three elements, then when the condition is fully satisfied, so that the three elements are randomly sorted.

So if you use the case function, it's best to use this method

Select *  fromParterwhereLvl='leader' Order  by( Case  whenName='Zhangsan'  Then 0  whenName='Lisi'  Then 1Name='Wangermazi'  Then 2 Else 3 End)

So, even if the name field has only three elements, it can be sorted according to our needs.

It is important to note that only the three elements we need are sorted according to our defined order, the remaining elements are randomly ordered, and if we need the rest of the elements to be sorted in a field or reverse order, simply add the comma field name to the last face.

Select *  fromParterwhereLvl='leader' Order  by( Case  whenName='Zhangsan'  Then 0  whenName='Lisi'  Then 1Name='Wangermazi'  Then 2 Else 3 End), id DESC

Decode function is the same

Select *  fromParterwhereLvl= 'leader' Order  byDecode (name,'Zhangsan',0,'Lisi',1,'Wangermazi',2,3), id DESC

As we know from the above example, those numbers are the positions of the elements, and if we want to put the elements we need in the last place.

You just have to change the numbers.

Case function

Select *  fromParterwhereLvl='leader' Order  by( Case  whenName='Zhangsan'  Then 3  whenName='Lisi'  Then 2Name='Wangermazi'  Then 1 Else 0 End). IDdesc

Decode function is the same

Select *  fromParterwhereLvl= 'leader' Order  byDecode (name,'Zhangsan',3,'Lisi',2,'Wangermazi',1,0), IDdesc

The powerful decode function also allows us to sort the rest of the elements according to our own ideas.

If we're going to end up with ' Zhangsan ' and ' Lisi ', and put ' Wangermazi ' first, then we can use two decode functions simultaneously

Select *  fromParterwhereLvl= 'leader' Order  byDecode (name,'Zhangsan',3,'Lisi',2,0), Decode (name,'Wangermazi',1), IDdesc

So we realized the Zhang San and John Doe respectively ranked the penultimate and the penultimate, Wang er the first, the remaining elements in reverse order of the ID.

So we realized that the elements we needed were sorted in a particular order.

"SQL" is sorted by specific required elements

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.