Usage of the oracle case when statement

Source: Internet
Author: User

1. The case when expression has two forms:
Copy codeThe Code is as follows: -- simple Case Function
CASE sex
WHEN '1' THEN 'male'
WHEN '2' THEN 'female'
ELSE 'others' END
-- Case search function
CASE
WHEN sex = '1' THEN 'male'
WHEN sex = '2' THEN 'female'
ELSE 'others' END

2. Usage of case when in different positions in the statement
2.1 select case when usage
Copy codeThe Code is as follows: SELECT grade, COUNT (case when sex = 1 THEN 1/* sex 1 for boys, 2 girls */
ELSE NULL
END) boys,
COUNT (case when sex = 2 THEN 1
ELSE NULL
END) Number of girls
FROM students group by grade;

2.2 where case when usage
Copy codeThe Code is as follows: 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 not like 'nothing %'
THEN 1
ELSE 0
END) = 1

2.3 group by case when usage
Copy codeThe Code is as follows: SELECT
Case when salary <= 500 THEN '1'
WHEN salary> 500 AND salary <= 600 THEN '2'
WHEN salary> 600 AND salary <= 800 THEN '3'
WHEN salary> 800 AND salary <= 1000 THEN '4'
Else null end salary_class, -- Alias Name
COUNT (*)
FROM Table_A
GROUP
Case when salary <= 500 THEN '1'
WHEN salary> 500 AND salary <= 600 THEN '2'
WHEN salary> 600 AND salary <= 800 THEN '3'
WHEN salary> 800 AND salary <= 1000 THEN '4'
Else null end;

3. Other IF-THEN-ELSE implementations
3.1 DECODE () function
Copy codeThe Code is as follows: select decode (sex, 'M', 'male', 'F', 'female ', 'unknown ')
From employees;

It seems that only Oracle provides this function, and ansi SQL is not supported, and the syntax is not CASE WHEN clear, which is not recommended for personal use.
3.2 Special implementation in the WHERE clause
Copy codeThe Code is as follows: SELECT T2. *, T1 .*
FROM T1, T2
WHERE (T2.COMPARE _ TYPE = 'A' AND T1.SOME _ type like 'nothing % ')
OR
(T2.COMPARE _ TYPE! = 'A' AND T1.SOME _ type not like 'nothing % ')

This method is also used in special cases. pay more attention to the logic and avoid mistakes.

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.