当前位置:网站首页>SQL数据分析之流程控制语句【if,case...when详解】
SQL数据分析之流程控制语句【if,case...when详解】
2022-07-01 23:59:00 【Chung丶无际鹰】
文章目录
零、写在前面
本文所有代码均是在SQL ZOO平台进行,数据也该平台下的world表,所有代码均已通过测试。
一、if单条件判断语句
1、语法结构:if(判断条件, 成立值, 不成立值)
select if(1=1,'Yes','No');
2、判断亚洲和非洲中国土面积大于200万的国家是否为人口大国(人口大于2亿)
select continent,name,if(population>200000000,'人口大国','小国') 人口规模
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
执行结果:
3、查询国土面积大于100万的亚洲国家中的总体经济水平(大于1万亿为高经济体)
select name,gdp,if(gdp>1000000000000,'高经济体','低经济体') as 总体经济
from world
where continent='Asia' and area>1000000

二、case…when多条件判断
1、单条件(if…else效果)
将上述if修改成case…when:
#判断亚洲和非洲中国土面积大于200万的国家是否为人口大国(人口大于2亿)
select continent,name,
CASE
WHEN population>200000000 THEN '人口大国'
ELSE '小国'
END AS 人口规模
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
#查询国土面积大于100万的亚洲国家中的总体经济水平(大于1万亿为高经济体)
select name,gdp,
CASE
when gdp>1000000000000 then '高经济体'
else '低经济体'
END as 总体经济
from world
where continent='Asia' and area>1000000
2、多条件(if…else if…else效果)
从gdp粗略判断国土面积大于50万,人口大于2000万的亚洲和欧洲国家的状态:
select continent,name,population,gdp,
CASE
WHEN gdp>=3000000000000 THEN '一等大国'
WHEN gdp>=2000000000000 and gdp<=3000000000000 THEN '二等大国'
WHEN gdp>=200000000000 and gdp<=1000000000000 THEN '三等大国'
ELSE '发展中大国'
END AS 国家状态
from world
where continent in ('Asia','Europe') and area>500000 and population>20000000
order by continent

3、常量用法(switch…case效果)
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2
…
else 要显示的值n或语句n;
例如:把人口数大于5000万国家的大洲名翻译成对应的中文
select name,population,
CASE continent
WHEN 'Asia' THEN '亚洲'
WHEN 'Afica' THEN '非洲'
WHEN 'South America' THEN '南美洲'
WHEN 'North America' THEN '北美洲'
WHEN 'Europe' THEN '欧洲'
ELSE '其他大洲'
END AS 所属大洲
from world
where population>=50000000

三、ifnull填充空值
Ifunull(字段名,“值1”)如果说这列的内容为null,则用’'来填充。如果不是null的话,则显示原有值。
select id, ifnull('score','缺考') as 分数;
若分数为null,则表示该学生缺考。
边栏推荐
- 记录一下大文件上传偶然成功偶然失败问题
- Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- Openwrt enable kV roaming
- Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
- 【.Net Core】程序相关各种全局文件
- [embedded system course design] a single key controls the LED light
- 第六章 数据流建模
- Windows installation WSL (II)
- 使用 pair 做 unordered_map 的键值
猜你喜欢

2021 robocom world robot developer competition - preliminary competition of higher vocational group

S32Kxxx bootloader之UDS bootloader

回顾数据脱敏系统
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

Chapter 6 data flow modeling

Selectively inhibiting learning bias for active sampling

Leetcode 96 différents arbres de recherche binaires

Concurrentskiplistmap -- principle of table skipping

PostgreSQL source code (57) why is the performance gap so large in hot update?

下载在线视频 m3u8使用教程
随机推荐
Shell custom function
Openwrt enable kV roaming
Redis RDB snapshot
Deep learning | three concepts: epoch, batch, iteration
攻防演练复盘
GCC compilation
Pytorch learning record
Difficult to get up syndrome (bit by bit greed)
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Linux centos7 installation Oracle11g super perfect novice tutorial
leetcode96不同的二叉搜索樹
使用 pair 做 unordered_map 的键值
Key points of security agreement
Resumption of attack and defense drill
Windows 7 安装MYSQL 错误:1067
在长城证券上买基金安全吗?
门级建模—课后习题
Selectively inhibiting learning bias for active sampling
比较通俗易懂的PID理解
Door level modeling - after class exercises