Case when then else function usage in sql statement

Case has two formats. Simple Case function and Case search function. --Simple
Case function
CASE sex
         WHEN '1' THEN'Male '
         WHEN '2' THEN'Female
'
ELSE'Other ' END --Case search function
CASE WHEN sex = '1' THEN'Male '
         WHEN sex = '2' THEN'Female'
ELSE'Other' END

These two methods can achieve the same function. The simple case function is written relatively concisely, but compared with the case search function, there are some limitations in terms of functions, such as writing judgments.
There is another problem that needs attention. The Case function only returns the first value that meets the condition, and the remaining Case part will be automatically ignored.
--For example, in the following piece of SQL, you will never get the result of the "second category"
CASE WHEN col_1 IN ('a','b') THEN'first category'
         WHEN col_1 IN ('a') THEN ' The second category'ELSE'other
' END

Let's take a look at what can be done with the Case function.

First, 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)
country (population)
China 600
United States 100
Canada 100
United Kingdom 200
France 300
Japan 250
Germany 200
Mexico 50
India 250

According to the population data of this country, count the population of Asia and North America. You should get the following result.
Continent Population
Asia 1100
North America 250
Other 700

What would you do if you want to solve this problem? Generating a View with a continent code is a solution, but it is difficult to dynamically change the way of statistics.
If you use the Case function, the SQL code is as follows:
SELECT SUM(population),
        CASE country
                WHEN'China' THEN'Asia'
                WHEN'India' THEN'Asia'
                WHEN'Japan' THEN'Asia'
                WHEN'United States' THEN'North America'
                WHEN 'Canada' THEN 'North America'
                WHEN 'Mexico' THEN 'North America'
        eLSE 'other' END
the FROM Table_A
the GROUP BY Country the CASE
                WHEN 'China' THEN 'Asian'
                WHEN 'India' THEN 'Asian'
                WHEN 'Japan'


                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 a SQL statement to complete the grouping of different conditions.

The following data are available
Country (country) Sex (population)
China 1 340
China 2 260
United States 1 45
United States 2 55
Canada 1 51
Canada 2 49
United Kingdom 1 40
United Kingdom 2 60

Grouped by country and gender, the results are as follows:
Country Male Female
China 340 260
United States 45 55
Canada 51 49
United Kingdom 40 60

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 complete 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 demonstrates the power of the Case function.

Third, use the Case function in Check.

Using Case function in Check is a very good solution in many situations. 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 examples.
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 you use Check and Case to express, as shown below
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)

If the conditions for female employees are met, male employees cannot enter.

 

Guess you like

Origin blog.csdn.net/My_SweetXue/article/details/109809833