SQLZOO:Window functions

1.Show the lastName, party and votes for the constituency ‘S14000024’ in 2017.

SELECT lastName, party, votes
  FROM ge
 WHERE constituency = 'S14000024' AND yr = 2017
ORDER BY votes DESC
  • 在题目给出的范例基础上无脑改就完事了

2.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

SELECT party, votes,
       RANK() OVER (ORDER BY votes DESC) as posn
  FROM ge
 WHERE constituency = 'S14000024' AND yr = 2017
ORDER BY party
  • 新语句:rank() over (order by columnA asc/desc),以列A为依据排序。asc代表最少的rank1,desc代表最多的rank1。

3.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).

SELECT yr,party, votes,RANK() OVER (PARTITION BY yr ORDER BY votes DESC) as posn
  FROM ge
 WHERE constituency = 'S14000021'
ORDER BY party,yr
  • 题目范例即为正确答案。在rank函数中加入了partition by yr语句,为每一yr的选举都分别按votes进行排名。

4.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.

SELECT constituency,party, votes, rank() over(partition by constituency order by votes desc) as posn
  FROM ge
 WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
   AND yr  = 2017
ORDER BY posn asc,constituency
  • ①between函数的变量不用纯数字也可以②order by语句运行顺序在select语句之后,可以运用select语句中设置的别名。

5.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.

SELECT constituency,party
  FROM (select ge.*,rank() over (partition by constituency 
  									 order by votes desc) rank 
  		  from ge 
		 WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
		   AND yr=2017) newtable
 where rank=1
 ORDER BY constituency
  • 说实话,sqlzoo可以随时提交代码并查看正确答案,这点可以令学习者直接根据答案来更改代码,是方便了学习,但是减少了思考的深度,可能会产生只是为了令答案正确而修改答案,缺少思考为何要这么去修改答案的过程。
  • rank=1这个条件要放在外面是因为rank这个别名在select语句中被定义,运行顺序在where语句之前,如果放在from语句中的select within select用的话,在别名是无法使用的。

6.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.

  • 得把题目翻译成人话,不然真解不了题。
  1. 首先我们要知道表格是英国历年大选的数据,数据内容包括每一年(yr)的每一个选区(constituency)中的哪些党派(group)的对应参选人(name)获得的选票(votes)。
  2. 另外需要大选的相关知识:在这里我们只需要知道每一个选区都会通过选举产生一名议员。(对应答案中,rank函数的partition语句需要根据yr和constituency来分组。)
  3. 在了解了表格内容后我们开始理解题意:我们需要展示2017年苏格兰地区的各党派最终都有多少席位(即有多少人当选)。
  4. 题目提示,苏格兰地区的选区是S开头的(constituencies start with ‘S’),可以用like语句解决。另外,当选人数可以用count联合rank函数解决,只需要令rank=1(选票最高者,即当选者),即可统计出共有多少个席位。
SELECT party,count(rank)
  FROM (select ge.*,rank() over (partition by yr,constituency 
  									order by votes desc) rank
		from ge
 		WHERE constituency like 'S%'
   		AND yr  = 2017) newtable
where rank=1
group by party
  • 没两把刷子真随便解不了这个题。学个代码还顺带被科普了一波英国大选…

你可能感兴趣的:(练习,SQLZOO)