Window functions

From SQLZoo
Language: Project:Language policy English  • 日本語

General Elections were held in the UK in 2015 and 2017. Every citizen votes in a constituency. The candidate who gains the most votes becomes MP for that constituency.

All these results are recorded in a table ge

yrfirstNamelastNameconstituencypartyvotes
2015IanMurrayS14000024Labour19293
2015NeilHayS14000024Scottish National Party16656
2015MilesBriggsS14000024Conservative8626
2015PhylMeyerS14000024Green2090
2015PramodSubbaramanS14000024Liberal Democrat1823
2015PaulMarshallS14000024UK Independence Party601
2015ColinFoxS14000024Scottish Socialist Party197
2017IanMURRAYS14000024Labour26269
2017JimEADIES14000024SNP10755
2017Stephanie Jane HarleySMITHS14000024Conservative9428
2017Alan ChristopherBEALS14000024Liberal Democrats1388

Warming up

Show the lastName, party and votes for the constituency 'S14000024' in 2017.

SELECTlastName,party,votes
FROMge
WHEREconstituency='E14000539'ANDyr=2017
ORDERBYvotesDESC
SELECTlastName,party,votes
FROMge
WHEREconstituency='S14000024'ANDyr=2017
ORDERBYvotesDESC

Who won?

You can use the RANK function to see the order of the candidates. If you RANK using (ORDER BY votes DESC) then the candidate with the most votes has rank 1.

Show the party and RANK for constituency S14000024 in 2017. List the output by party
set sql_mode = replace(@@sql_mode,'ONLY_FULL_GROUP_BY','');
SELECTparty,votes,
RANK()OVER(ORDERBYvotesDESC)asposn
FROMge
WHEREconstituency='E14000539'ANDyr=2017
ORDERBYvotes
SELECTparty,votes,
RANK()OVER(ORDERBYvotesDESC)asposn
FROMge
WHEREconstituency='S14000024'ANDyr=2017
ORDERBYparty

PARTITION BY

The 2015 election is a different PARTITION to the 2017 election. We only care about the order of votes for each year.

Use PARTITION to show the ranking of each party in S14000021 in each year. Include yr, party, votes and ranking (the party with the most votes is 1).
set sql_mode = replace(@@sql_mode,'ONLY_FULL_GROUP_BY','')
SELECTyr,party,votes,
RANK()OVER(PARTITIONBYyrORDERBYvotesDESC)asposn
FROMge
WHEREconstituency='S14000021'
ORDERBYparty,yr
SELECTyr,party,votes,
RANK()OVER(PARTITIONBYyrORDERBYvotesDESC)asposn
FROMge
WHEREconstituency='S14000021'
ORDERBYparty,yr

Edinburgh Constituency

Edinburgh constituencies are numbered S14000021 to S14000026.

Use PARTITION BY constituency to show the ranking of each party in Edinburgh in 2017. Order your results so the winners are shown first, then ordered by constituency.
set sql_mode = replace(@@sql_mode,'ONLY_FULL_GROUP_BY','');
SELECTconstituency,party,votes
FROMge
WHEREconstituencyBETWEEN'S14000021'AND'S14000026'
ANDyr=2017
ORDERBYconstituency,votesDESC
SELECTconstituency,party,votes,
RANK()OVER(PARTITIONBYconstituencyORDERBYvotesDESC)
ASposn
FROMge
WHEREconstituencyBETWEEN'S14000021'AND'S14000026'
ANDyr=2017
ORDERBYposn,constituency

Winners Only

You can use SELECT within SELECT to pick out only the winners in Edinburgh.

Show the parties that won for each Edinburgh constituency in 2017.
set sql_mode = replace(@@sql_mode,'ONLY_FULL_GROUP_BY','');
SELECTconstituency,party,votes
FROMge
WHEREconstituencyBETWEEN'S14000021'AND'S14000026'
ANDyr=2017
ORDERBYconstituency,votesDESC
SELECTconstituency,party
FROM(
SELECTconstituency,party,
RANK()OVER(PARTITIONBYconstituencyORDERBYvotesDESC)
ASposn
FROMge
WHEREconstituencyBETWEEN'S14000021'AND'S14000026'
ANDyr=2017
)ASed
WHEREposn=1

Scottish seats

You can use COUNT and GROUP BY to see how each party did in Scotland. Scottish constituencies start with 'S'

Show how many seats for each party in Scotland in 2017.
set sql_mode = replace(@@sql_mode,'ONLY_FULL_GROUP_BY','');
SELECTconstituency,party,votes
FROMge
WHEREconstituencyBETWEEN'S14000021'AND'S14000026'
ANDyr=2017
ORDERBYconstituency,votesDESC
SELECTparty,COUNT(1)
FROM(
SELECTconstituency,party,
RANK()OVER(PARTITIONBYconstituencyORDERBYvotesDESC)
ASposn
FROMge
WHEREconstituencyLIKE'S%'
ANDyr=2017
)ASed
WHEREposn=1
GROUPBYparty
DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects
  • Served by: sage at 2026年01月05日T11:06