当前位置:网站首页>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 .
边栏推荐
- Niuke - Practice 101 - reasoning clown
- Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
- Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
- Which securities company is the best to open a stock account? Is there a security guarantee
- Difficult to get up syndrome (bit by bit greed)
- 一个实习生的CnosDB之旅
- SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
- [embedded system course design] a single key controls the LED light
- Jielizhi, production line assembly link [chapter]
- Use pair to do unordered_ Key value of map
猜你喜欢

RPA tutorial 01: Excel automation from introduction to practice
![[QT] solve the problem that QT MSVC 2017 cannot compile](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[QT] solve the problem that QT MSVC 2017 cannot compile

Key points of security agreement

Why does blocprovider feel similar to provider?

algolia 搜索需求,做的快自闭了...

Deep learning | three concepts: epoch, batch, iteration

SQL Server 安装指南

Openvino model performance evaluation tool DL workbench

How to solve the image pop-up problem when pycharm calls Matplotlib to draw
Linux CentOS7安装Oracle11g的超完美新手教程
随机推荐
. env. XXX file, with constant, but undefined
ADO.NET之SqlDataAdpter对象
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
mysql之B tree 以及 B+tree
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
牛客-练习赛101-推理小丑
golang中的iota
Use vb Net to convert PNG pictures into icon type icon files
Dongge cashes in and the boss retires?
ADO. Net SqlConnection object usage summary
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
写给当前及未来博士研究生一些建议整理分享
Shell process control
Key points of security agreement
启牛学院开户安全的吗?开户怎么开?
The difference between timer and scheduledthreadpoolexecutor
【QT】对于Qt MSVC 2017无法编译的问题解决
多表操作-一对一,一对多与多对多
The origin of usb-if Association and various interfaces
求逆序数的三个方法