SQL Server complex ordering (order by case)

Source: Internet
Author: User
Tags getdate
/*
Table Sysid Self-increment primary key SCRO score Oper operation time SCRO score > 5 According to the fractional descending, the score is less than or equal to 5 according to the operation time Ascending; >5 in front of the =5.
*/select * FROM Test order by case-SCRO > 5 then 1 else 0 End desc, case-scro > 5 then scro elsedatediff (M Inute,oper,getdate ()) End desc--The first case put "5 and" =5 Whole, sort, the second one in order to satisfy (the score is less than or equal to 5 in accordance with the operation time Ascending) condition cannot be the same level of an ascending one descending, so by DATED The "descending" way of the IFF (Minute,oper,getdate ()), which implements the ascending order of the dates.

SQL Server complex ordering (order by case)

Related Article

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.