当前位置:网站首页>Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
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 operation , Data is also available under the platform world Table and some data tables provided by other platforms , All the codes have passed the test .
One 、 Subquery basic usage
1、 explain
The subquery itself is a complete query statement , Then use English brackets ( The package is nested in the main query word hydatid ,
Subqueries can be nested at multiple levels. The most commonly used subqueries are used in from and where clause .
2、 Inquire about gdp Higher than the names of all countries in Europe , There are some countries gdp May be null, Need to be excluded
select name
from world
where gdp is not null
and gdp>=(select max(gdp) from world where continent='Europe')
Be careful : Remember the subquery select Put brackets in it , And the returned value must be a value (max(gdp)), Instead of a list of values (gdp)
3、 Inquiry with Australia Australia And Chile Chile Countries and continents of the same continent
select continent,name
from world
where continent in (select continent from world where name in('Australia','Chile'))
order by continent,name
4、 Detailed explanation of subquery
(1) Subquery is a complete query statement that can run normally and independently , Then take the query result of the sub query as part of the main query , Therefore, the sub query takes precedence over the main query
(2) practice 1 Is a subquery with a comparison operator , The subquery is required to be a scalar subquery , That is, the sub query result is one line - - Column ( amount to - One cell )
(3) practice 2 It's with in Subqueries for keywords , Subqueries are required to be column subqueries , That is, the sub query result is multiple rows and one column ( Single column )
(4)where The subquery in clause is applicable to query conditions that cannot be achieved in one step , You need to make a one-step query to get the results , Condition judgment based on the query result , Equivalent to when we can't get there directly , Need to transfer
Two 、 Subquery comprehensive usage ( problem )
1、 Inquire about 2017 All constituencies in Edinburgh were elected to their constituencies (constituency) Its team (party) , The known constituency number of Edinburgh is S14000021 to S14000026, The elected member is the candidate with the highest number of votes in each constituency
First step : Use the window function to sort the candidates in each constituency
select constituency,party,votes,
rank() over(PARTITION BY consitituency ORDER BY votes desc) as RV
from ge
where constituency between 'S14000021' and 'S14000026' and yr=2017
group by constituency,party,votes
The second step : Take the window function as from Subquery of , Select the candidate with the highest number of votes in the current constituency ( MPS )
select RK.constituency,RK.party
from (
select constituency,party,votes,
rank() over(PARTITION BY constituency ORDER BY votes desc) as RV
from ge
where constituency between 'S14000021' and 'S14000026' and yr=2017
group by constituency,party,votes
) as RK
where RK.RV=1
2、 Query the population (population) More than Canada (Canada) But less than Poland (Poland) The country , The results show that the country names of these countries (name) And population (population)
select name,population
from world
where population > (
select population from world where name='Canada'
) and population < (
select population from world where name='Poland'
)
3、 The population of all countries is less than 25000000 The continent , And its country name (name) And population (population)
select continent,name,population
from world
where continent not in (
select distinct continent from world where population>25000000
)
order by continent;
a key : The population of all countries is less than , It's hard to judge , But it's easy to put the existing country bigger than the population Find out the continent , Again not in These continents
4、 Query in Europe (Europe) Per capita gdp Greater than Britain (United Kingdom) The name of your country
select name
from world
where continent='Europe'
and (gdp/population)>(
select (gdp/population) from world where name='United Kingdom'
)
5、 Query the largest country on each continent ( Judge area), Show the continent (continent), The name of the country (name) And area (area)
First step : Rank the land area of each continent
select continent,name,area,
rank() over(PARTITION BY continent ORDER BY area desc) as RA
from world
group by continent,name,area
The second step : Regard the countries with good rankings as from Subquery , And then select the countries that rank first on each continent
select RK.continent,RK.name,RK.area
from (
select continent,name,area,
rank() over(PARTITION BY continent ORDER BY area desc) as RA from world
group by continent,name,area) as RK
where RK.RA=1
order by RK.continent;
6、 Check the number of new patients in France and Italy every day and rank them from high to low , Query results by country name , As of ( year month * Japan ), New cured people , Sort by rank
First step : Check the country names of France and Italy , Closing date , The number of new cured people every day ( Subtract the previous day from the next day )
select name
,date_format(whn,'%Y year %m month %d Japan ') Closing date
,(recovered-lag(recovered,1) over(PARTITION BY name ORDER BY whn)) The number of new cured people every day
from covid
where name in ('France','Italy')
group by name,whn,recovered
The second step : Take the result of the first step as from Subquery of , select name, Closing date , The number of new cured people every day , And rank according to the number of new cured people every day
select name
, Closing date
, The number of new cured people every day
,rank() over(PARTITION BY RE.name ORDER BY RE. The number of new cured people every day desc) ranking
from (
select name
,date_format(whn,'%Y year %m month %d Japan ') Closing date
,(recovered-lag(recovered,1) over(PARTITION BY name ORDER BY whn)) The number of new cured people every day
from covid
where name in ('France','Italy')
group by name,whn,recovered
) as RE
group by RE.name,RE. Closing date ,RE. The number of new cured people every day
order by ranking
Written in the back : The article is summarized in elder martial brother zidai's course :https://www.bilibili.com/video/BV1ZM4y1u7uF?p=4
Some modifications and verifications have been made on the basis of this course .
边栏推荐
- The origin of usb-if Association and various interfaces
- JPA handwritten SQL, received with user-defined entity classes
- 2021 robocom world robot developer competition - preliminary competition of higher vocational group
- Is it safe to choose mobile phone for stock trading account opening in Beijing?
- mysql:insert ignore、insert和replace区别
- 回顾数据脱敏系统
- 使用VB.net将PNG图片转成icon类型图标文件
- Which app is better and more secure for stock mobile account opening
- Leetcode 96 différents arbres de recherche binaires
- golang中的iota
猜你喜欢
leetcode96不同的二叉搜索树
【QT】对于Qt MSVC 2017无法编译的问题解决
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
Linux centos7 installation Oracle11g super perfect novice tutorial
如何提升数据质量
Windows10 install WSL (I) (wslregisterdistribution error)
Review data desensitization system
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Windows installation WSL (II)
The essence of software architecture
随机推荐
vue 强制清理浏览器缓存
Correlation - intra group correlation coefficient
const // It is a const object...class nullptr_t
S32Kxxx bootloader之UDS bootloader
记录一下大文件上传偶然成功偶然失败问题
How to realize parallel replication in MySQL replication
[untitled]
Jielizhi, production line assembly link [chapter]
MySQL: the difference between insert ignore, insert and replace
RPA教程01:EXCEL自动化从入门到实操
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Is it safe to buy funds on Great Wall Securities?
mysql之B tree 以及 B+tree
一个实习生的CnosDB之旅
leetcode96不同的二叉搜索樹
基于全志H3的QT5.12.9移植教程
[QT] QT cannot find a solution to the compiler using msvc2017
[embedded system course design] a single key controls the LED light
求逆序数的三个方法
Deep learning | three concepts: epoch, batch, iteration