Real-combat basic Skills (-------) SQL using case and then multi-criteria judgment

Source: Internet
Author: User

Case
When condition 1 then result 1
When condition 2 then result 2
When condition 3 then result 3
When condition 4 then result 4
.........
When condition n then result n
ELSE result X
END
Case has two formats. Simple case function and case search function.
--Simple Case function
Case Sex
When ' 1 ' Then ' men '
When ' 2 ' then ' women '
Else ' other ' END
--case search function
case If sex = ' 1 ' Then ' man '
When sex = ' 2 ' Then ' women '
Else ' other ' END
For example:
SELECT ID, name, CJ, (case at CJ < Then ' fail ' when CJ between and all then ' good ' when CJ > all then ' Excellent ' END) A S State
From stud

In both of these ways, the same functionality can be achieved. The simple case function is relatively concise, but there are some limitations in function, such as write-judgement, compared to the search function.
There is also a problem to be aware that the case function returns only the first qualifying value, and the remaining case section is automatically ignored.
--for example, the following SQL, you can never get the result of "type two"
Case is col_1 in (' A ', ' B ') then ' first Class '
When Col_1 in (' a ') then ' second class '
Else ' other ' END

Such as:
Case-Created >[email protected] and Created<[email protected] then 1 else 0 end

Real-combat basic Skills (-------) SQL using case and then multi-criteria judgment

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.