当前位置:网站首页>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 .
边栏推荐
- ADO.NET 之sqlConnection 对象使用摘要
- Asp .NetCore 微信订阅号自动回复之文本篇
- SQL Server 安装指南
- SQL数据分析之流程控制语句【if,case...when详解】
- 写给当前及未来博士研究生一些建议整理分享
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- The essence of software architecture
- Kubernetes resource object introduction and common commands (III)
- Three methods of finding inverse numbers
- [QT] QT cannot find a solution to the compiler using msvc2017
猜你喜欢

基于全志H3的QT5.12.9移植教程

TS initial use, TS type

.env.xxx 文件,加了常量,却undefined

如何提升数据质量

mysql之B tree 以及 B+tree

The origin of usb-if Association and various interfaces

【QT】Qt 使用MSVC2017找不到编译器的解决办法

kubernetes资源对象介绍及常用命令(三)

Write some suggestions to current and future doctoral students to sort out and share

比较通俗易懂的PID理解
随机推荐
比较通俗易懂的PID理解
【CMake】Qt creator 里面的 cmake 配置
cookie、session、tooken
Operate database transactions with jpatractionmanager
How excel opens CSV files with more than one million lines
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Door level modeling - after class exercises
在证券账户上买基金安全吗?哪里可以买基金
USB-IF协会与各种接口的由来
Openwrt enable kV roaming
UDS bootloader of s32kxxx bootloader
【模板】自适应辛普森积分
ADO. Net SqlConnection object usage summary
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
RPA教程01:EXCEL自动化从入门到实操
Algolia's search needs are almost closed
Overview of edge calculation
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
北京炒股开户选择手机办理安全吗?