Sort values in different columns based on conditions.

Source: Internet
Author: User

Sort values in different columns based on conditions.

Sometimes the ordering requirements are complex. For example, if a leader is more interested in employees with salaries between 1000 and 2000 yuan, the employees with salaries in this range must be at the top, for priority.

Create a test table:

Create or replace view v ASSELECT 'martin 'AS ename, 950 AS sal from dualunion allselect 'werd' AS ename, 1250 AS sal from dualunion allselect 'terner' AS ename, 1500 AS sal from dualunion allselect 'allen 'AS ename, 1600 AS sal from dualunion allselect 'James' AS ename, 2850 AS sal from dual; SELECT * FROM v;

The SQL code is as follows:

SELECT * FROM v order by case when sal BETWEEN 1000 AND 2000 THEN 1 ELSE 2 END, sal;

The execution result is as follows:

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.