当前位置:网站首页>Flow control statement of SQL data analysis [if, case... When detailed]
Flow control statement of SQL data analysis [if, case... When detailed]
2022-07-02 00:05:00 【Chung, boundless Eagle】
List of articles
zero 、 Write it at the front
All the code in this article is in SQL ZOO platform Conduct , Data is also available under the platform world surface , All the codes have passed the test .
One 、if Single condition judgment statement
1、 Grammatical structure :if( Judge the condition , Establishment value , Invalid value )
select if(1=1,'Yes','No');
2、 It is judged that the soil area of China in Asia and Africa is larger than 200 Is a country with a large population ( The population is larger than 2 Billion )
select continent,name,if(population>200000000,' A large population ',' Small country ') Population size
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
Execution results :
3、 Query land area is larger than 100 The overall economic level of million Asian countries ( Greater than 1 Trillion is a high economy )
select name,gdp,if(gdp>1000000000000,' High economy ',' Low economies ') as General economy
from world
where continent='Asia' and area>1000000
Two 、case…when Multi condition judgment
1、 Single condition (if…else effect )
Put the above if Modified into case…when:
# It is judged that the soil area of China in Asia and Africa is larger than 200 Is a country with a large population ( The population is larger than 2 Billion )
select continent,name,
CASE
WHEN population>200000000 THEN ' A large population '
ELSE ' Small country '
END AS Population size
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
# Query land area is larger than 100 The overall economic level of million Asian countries ( Greater than 1 Trillion is a high economy )
select name,gdp,
CASE
when gdp>1000000000000 then ' High economy '
else ' Low economies '
END as General economy
from world
where continent='Asia' and area>1000000
2、 Multiple conditions (if…else if…else effect )
from gdp It is roughly judged that the land area is larger than 50 ten thousand , The population is larger than 2000 The state of ten thousand Asian and European countries :
select continent,name,population,gdp,
CASE
WHEN gdp>=3000000000000 THEN ' First class big country '
WHEN gdp>=2000000000000 and gdp<=3000000000000 THEN ' A second-class power '
WHEN gdp>=200000000000 and gdp<=1000000000000 THEN ' Third class big country '
ELSE ' Large developing countries '
END AS State of the country
from world
where continent in ('Asia','Europe') and area>500000 and population>20000000
order by continent
3、 Constant usage (switch…case effect )
case The field or expression to determine
when Constant 1 then The value to display 1 Or words 1
when Constant 2 then The value to display 2 Or words 2
…
else The value to display n Or words n;
for example : Make the population larger than 5000 The continent names of ten thousand countries are translated into the corresponding Chinese
select name,population,
CASE continent
WHEN 'Asia' THEN ' Asia '
WHEN 'Afica' THEN ' Africa '
WHEN 'South America' THEN ' South America '
WHEN 'North America' THEN ' In North America '
WHEN 'Europe' THEN ' The European '
ELSE ' Other continents '
END AS Continent
from world
where population>=50000000
3、 ... and 、ifnull Fill in empty values
Ifunull( Field name ,“ value 1”) If the content of this column is null, Then use ’' Fill in . If not null Words , Then the original value is displayed .
select id, ifnull('score',' miss an exam ') as fraction ;
If the score is null, It means that the student is absent .
边栏推荐
- Leetcode 96 différents arbres de recherche binaires
- Is it safe to choose mobile phone for stock trading account opening in Beijing?
- SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
- Overview of edge calculation
- Learn online case practice
- JPA handwritten SQL, received with user-defined entity classes
- 使用多线程Callable查询oracle数据库
- .env.xxx 文件,加了常量,却undefined
- Algolia's search needs are almost closed
- Use pair to do unordered_ Key value of map
猜你喜欢
Material design component - use bottomsheet to show extended content (I)
Linux CentOS7安装Oracle11g的超完美新手教程
PyTorch学习记录
2021 robocom world robot developer competition - semi finals of higher vocational group
Learn online case practice
边缘计算概述
RPA教程01:EXCEL自动化从入门到实操
使用VB.net将PNG图片转成icon类型图标文件
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
The origin of usb-if Association and various interfaces
随机推荐
Is it safe to choose mobile phone for stock trading account opening in Beijing?
一个实习生的CnosDB之旅
Review data desensitization system
Correlation - intra group correlation coefficient
13 MySQL constraint
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Relatively easy to understand PID understanding
SQL Server 安装指南
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
Learn online case practice
Use pair to do unordered_ Key value of map
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Niuke - Practice 101 - reasoning clown
Use the htaccess file to prohibit the script execution permission in the directory
RPA教程01:EXCEL自动化从入门到实操
Shell process control
[QT] test whether QT can connect to the database
vs2015 AdminDeployment.xml
Dongge cashes in and the boss retires?
ADO. Net SqlDataAdapter object