Use of case when in Oracle

Source: Internet
Author: User

Http://www.cnblogs.com/eshizhan/archive/2012/04/06/2435493.html

1. Two forms of the case-when expression:

--Simple case function case  sex when  ' 1 ' then ' Male ' when  ' 2 ' Then ' Women '  Else ' other ' END  --case search function  casewhen sex = ' 1 ' Then ' man ' when  sex = ' 2 ' Then ' women ' else '  other ' END  

2. When the case is used in different locations of the SQL statement:

2.1 Select Case

SELECT   grade, count (case if sex = 1 then 1      /*sex 1 for boys, 2 girls *                       /ELSE NULL                       END) Number of boys,                count (case when s ex = 2 then 1                       ELSE NULL                       END) Number of schoolgirls from    students GROUP by grade;
2.2 where case

SELECT t2.*, t1.* from   T1, T2  WHERE (case when t2.compare_type = ' A ' and                   T1. Some_type like ' nothing% ' then                1 when              t2.compare_type! = ' A ' and                   T1. Some_type ' nothing% ' then                1              ELSE 0           END) = 1
2.3 Group by case

SELECT case when  salary <= "1" when  salary > Salary <= and  "2" when  salary > Salary <=  3 ' when  salary > Salary <= and ' 4 '  ELSE NULL END Salar Y_class,--alias name Count (*) from table_a GROUP by case when  salary <= and ' 1 ' when  salary &G T and salary <=  2 ' when  salary > Salary <=, then  ' 3 ' when  salary > Salary <= and 4 '  
3. Other implementations of If-then-else

3.1 Decode function:

Select decode (Sex, ' M ', ' Male ', ' F ', ' Female ', ' Unknown ') from   employees;



Use of case when in Oracle

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.