当前位置:网站首页>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 .
边栏推荐
猜你喜欢

It's nothing to be utilitarian!

使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object

BlocProvider为什么感觉和Provider很相似?

PyTorch学习记录

Download the online video m3u8 tutorial

【QT】對於Qt MSVC 2017無法編譯的問題解决

Use vb Net to convert PNG pictures into icon type icon files

The origin of usb-if Association and various interfaces

Pytorch learning record

SQL数据分析之子查询的综合用法和案例题【耐心整理】
随机推荐
Correlation - intra group correlation coefficient
起床困难综合症(按位贪心)
2021 robocom world robot developer competition - preliminary competition of undergraduate group
JPA handwritten SQL, received with user-defined entity classes
比较通俗易懂的PID理解
2021 robocom world robot developer competition - semi finals of higher vocational group
[QT] qtcreator uninstall and installation (abnormal state)
kubernetes资源对象介绍及常用命令(三)
Openvino model performance evaluation tool DL workbench
[QT] QT cannot find a solution to the compiler using msvc2017
ADO. Net SqlDataAdapter object
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
[C #] dependency injection and Autofac
Jielizhi, production line assembly link [chapter]
2021 robocom world robot developer competition - preliminary competition of higher vocational group
UDS bootloader of s32kxxx bootloader
const // It is a const object... class nullptr_ t
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Selectively inhibiting learning bias for active sampling