How to use SQL Case when

Case comes in two formats. Simple Case functions and Case search functions.

--Simple Case function
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE 'other' END
--Case search function
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE 'other' END



These two methods can achieve the same function. The writing method of the simple case function is relatively simple, but compared with the case search function, there are some limitations in functions, such as writing judgments.
There is also a problem to pay attention to, the Case function only returns the first value that meets the condition, and the rest of the Case part will be automatically ignored.

--For example, in the following SQL, you can never get the result of "second type"
CASE WHEN col_1 IN ( 'a', 'b') THEN 'first class'
WHEN col_1 IN ('a') THEN 'Second class'
ELSE 'other' END



Let's take a look at what you can do with the Case function.

One, the known data is grouped and analyzed in another way.

There are the following data: (In order to see more clearly, I did not use the country code, but directly used the country name as the Primary Key)


According to the population data of this country, count the population of Asia and North America. You should get the following result.



To solve this problem, what would you do? Generating a View with Continent Code is a solution, but it is difficult to dynamically change the way of statistics.
If the Case function is used, the SQL code is as follows:
SELECT  SUM(population),
CASE country
WHEN 'China' THEN 'Asia'
WHEN 'India' THEN 'Asian'
WHEN 'Japan' THEN 'Asian'
WHEN 'US' THEN 'North America'
WHEN 'Canada' THEN 'North America'
WHEN 'Mexico' THEN 'North America'
ELSE 'other' END
FROM    Table_A
GROUP BY CASE country
WHEN 'China' THEN 'Asia'
WHEN 'India' THEN 'Asian'
WHEN 'Japan' THEN 'Asian'
WHEN 'US' THEN 'North America'
WHEN 'Canada' THEN 'North America'
WHEN 'Mexico' THEN 'North America'
ELSE 'other' END;



Similarly, we can also use this method to judge the level of wages and count the number of people in each level. The SQL 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,
COUNT(*)
FROM    Table_A
GROUP BY
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;


Second, use one SQL statement to complete the grouping of different conditions. The following

data are grouped by country and gender, and the results are as follows: Under normal circumstances, UNION can also be used to query with one statement. But that increases the consumption (two Select parts), and the SQL statement will be longer. The following is an example of using the Case function to accomplish this function







SELECT country,
SUM( CASE WHEN sex = '1' THEN
population ELSE 0 END), -- male population
SUM( CASE WHEN sex = '2' THEN
population ELSE 0 END) -- female population
FROM  Table_A
GROUP BY country;


In this way, we use Select to complete the output form of the two-dimensional table, which fully shows the power of the Case function.

Third, use the Case function in Check.

Using the Case function in Check is a very good solution in many cases. There may be many people who don't use Check at all, so I suggest you try to use Check in SQL after reading the following example.
Let's take an example
Company A. This company has a rule that the salary of female employees must be higher than 1,000 yuan. If it is represented by Check and Case, it is as follows

CONSTRAINT check_salary CHECK
( CASE WHEN sex = '2'
THEN CASE WHEN salary > 1000
THEN 1 ELSE 0 END
ELSE 1 END = 1 )



If you simply use Check, as shown below

CONSTRAINT check_salary CHECK
( sex = '2' AND salary > 1000 )



The conditions for female staff are met, but male staff cannot enter.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326457763&siteId=291194637