Home > Database > SQL > body text

Usage of Case When in SQL

angryTom
Release: 2022-03-29 16:19:14
Original
182515 people have browsed it

In SQL, the "Case When" statement is used to select and judge. During execution, the conditions are first judged, and then corresponding operations are made based on the judgment results; syntax "CASE field WHEN condition 1 THEN operation 1 WHEN Condition 2 THEN operation 2...ELSE operation n END;".

Usage of Case When in SQL

The operating environment of this tutorial: Windows 7 system, Microsoft SQL Server 2016 version, Dell G3 computer.

Usage of case when in SQL

case when is similar to the if else judgment and switch case statement in programming languages. When this statement is executed, the condition is first judged, and then the corresponding operation is made based on the judgment result.

Case has two formats: simple Case function and Case search function.

Simple Case function:

CASE sex
WHEN ‘1’ THEN ‘男’
WHEN ‘0’ THEN ‘女’
ELSE ‘其他’ END
Copy after login

Case search function:

CASE WHEN sex = ‘1’ THEN ‘男’
WHEN sex = ‘0’ THEN ‘女’
ELSE ‘其他’ END
Copy after login

Obviously, the simple Case function is more concise, but it is only suitable for such single fields Single value comparison, and the advantage of the Case search function is that it is applicable to all comparison situations.

There is another issue that needs attention. After the Case function meets a certain condition, the remaining conditions will be automatically ignored. Therefore, even if multiple conditions are met, only the first one will be recognized during execution. a condition.

(PHP Chinese website, there are a lot of free SQL tutorials, everyone is welcome to learn!)

When using CASE WHEN, you can treat it as a logical For anonymous fields, the field value is confirmed based on conditions. When you need to use the field name, you can use as to define an alias. This is still very abstract. Look at the use case of CASE WHEN below to make it clear.

Usage scenarios

1. Known data can be grouped and analyzed in a certain way.

Usage of Case When in SQL

Based on the population data of this country, count the population of Asia and North America. Use the following SQL:

SELECT  CASE country
WHEN '中国'     THEN '亚洲'
WHEN '印度'     THEN '亚洲'
WHEN '日本'     THEN '亚洲'
WHEN '美国'     THEN '北美洲'
WHEN '加拿大'  THEN '北美洲'
WHEN '墨西哥'  THEN '北美洲'
ELSE '其他' END as '洲' , SUM(population) as '人口'
FROM test
GROUP BY CASE country
WHEN '中国'     THEN '亚洲'
WHEN '印度'     THEN '亚洲'
WHEN '日本'     THEN '亚洲'
WHEN '美国'     THEN '北美洲'
WHEN '加拿大'  THEN '北美洲'
WHEN '墨西哥'  THEN '北美洲'
ELSE '其他' END;
Copy after login

Usage of Case When in SQL

The two CASE WHEN here are equivalent to one field, but it is worth mentioning that the THEN value of the second CASE WHEN There is no need to specify which continent it is, it is just used to group records, so the value after THEN can only distinguish these three types of records. GROUP BY can also be written as:

GROUP BY CASE country
WHEN '中国'     THEN 0
WHEN '印度'     THEN 0
WHEN '日本'     THEN 0
WHEN '美国'     THEN 1
WHEN '加拿大'  THEN 1
WHEN '墨西哥'  THEN 1
ELSE 2 END;
Copy after login

2. Use a SQL statement Complete groupings under different conditions.

There is the following data:

Usage of Case When in SQL

Use the Case function to complete grouping by country and gender. Use the following SQL:

SELECT country,
SUM( CASE WHEN sex = '1' THEN population ELSE 0 END  ),  --男性人口
SUM( CASE WHEN sex = '2' THEN population ELSE 0 END )   --女性人口
FROM  Table_A
GROUP BY country;
Copy after login

Get the following results:

Usage of Case When in SQL

Explain the first CASE WHEN:

CASE WHEN sex = '1' THEN
population ELSE 0 END
Copy after login

When the sex of the record is 1, the value of this field is the population value of the record, otherwise it is 0, so the male population of a country can be calculated.

For more SQL and other Introduction to Programming tutorials, please continue to pay attention to the PHP Chinese website! !         

The above is the detailed content of Usage of Case When in SQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!