我怎么能写这个postgreSQL rank() - 查询?

时间:2017-10-20 08:36:26

标签: postgresql

我有一个包含足球队的桌子,其中包含以下列名称:

Teamname  --- Region  ---  Score

我想按分数对我的足球队进行排名。我设法做了这样的RANK()查询:

SELECT teamname, region, RANK() OVER(ORDER BY score) 
as rankingTotal FROM soccerscoretable

现在我希望按地区和总排名获得排名。

查询的预期结果是一个答案,它给出了以下内容:

  • 团队名称
  • 区域
  • 得分
  • 总排名
  • 团队区域的排名。

这就是我现在尝试编写该查询的方法。 它不起作用。我迷失在树林里。

SELECT teamname, region, RANK() OVER(ORDER BY score) as rankingtotal, rankingRegion
FROM soccerscoretable,
(SELECT RANK() OVER(ORDER BY score) as rankregion 
 from soccerscoretable where region = 'The regions name') AS rankingRegion

SQL Fiddle here

示例数据:

| teamname | region  | score |
|----------|---------|-------|
| team5    | region3 | 50    |
| team1    | region1 | 100   |
| team2    | region2 | 200   |
| team4    | region2 | 250   |
| team3    | region1 | 300   |

预期输出:

| teamname | region  | score | rankingtotal | rankingregion |
|----------|---------|-------|--------------|---------------|
| team5    | region3 | 50    | 1            | 1             |
| team1    | region1 | 100   | 2            | 1             |
| team2    | region2 | 200   | 3            | 1             |
| team4    | region2 | 250   | 4            | 2             |
| team3    | region1 | 300   | 5            | 2             |

任何帮助将不胜感激! 感谢

0 个答案:

没有答案