每年获得排名(行号)

时间:2019-02-23 15:15:19

标签: sql rank partition

我有一个包含排放和人口的数据库。我正在使用以下查询来获取人均排放量和每个国家/地区的排名:

SELECT 
      cp.year as year, 
      emissions, 
      ci.id as countryid, 
      country_name, 
      country_iso_code, 
      emissions / population as per_capita, 
      CASE WHEN emissions / population IS NULL THEN NULL ELSE RANK() OVER (ORDER 
      BY CASE WHEN emissions / population IS NULL THEN 1 ELSE 0 END, emissions / 
      population DESC) END AS rank 
FROM country_emissions ce 
      RIGHT JOIN 
                 country_info ci ON (ci.countryid = ce.countryid) 
      RIGHT JOIN 
                 country_population cp ON (ce.countryid = cp.countryid) AND (cp.year = ce.year) 
WHERE 
      cp.year = 2010 
GROUP BY 
      ce.countryid, ce.year, ce.emissions, ci.id, ci.countryid, ci.country_iso_code, ci.country_name, cp.id, cp.countryid, cp.year, cp.population 
ORDER BY 
    emissions / population;

它将给我以下结果:

 year |  emissions  | countryid |          country_name          | country_iso_code |         per_capita         | rank
------+-------------+-----------+--------------------------------+------------------+----------------------------+------
 2010 |     212.686 |        14 | Burundi                        | BDI              | 0.000024260031732887110996 |  201
 2010 |    2020.517 |        40 | Congo, Dem. Rep.               | COD              | 0.000031314550846568314439 |  200
 2010 |     517.047 |       187 | Chad                           | TCD              | 0.000043496106148444352170 |  199
 2010 |     612.389 |       174 | Somalia                        | SOM              | 0.000050807074589095381376 |  198
 2010 |     590.387 |       165 | Rwanda                         | RWA              | 0.000057616483205264607379 |  197
 2010 |     264.024 |        32 | Central African Republic       | CAF              | 0.000059350908447181931090 |  196

现在问这个问题。如果我们将rank排除在查询之外,有没有办法每年和每个国家/地区获取per_capitaWHERE cp.year = 2010

结果将是这样的:

 year |  emissions  | countryid |          country_name          | country_iso_code |         per_capita         | rank
------+-------------+-----------+--------------------------------+------------------+----------------------------+------
 2010 |     212.686 |        14 | Burundi                        | BDI              | 0.000024260031732887110996 |  201
 2009 |     210.686 |        14 | Burundi                        | BDI              | 0.000024260031732887110996 |  200
 2010 |    2020.517 |        40 | Congo, Dem. Rep.               | COD              | 0.000031314550846568314439 |  200
 2011 |    2020.517 |        40 | Congo, Dem. Rep.               | COD              | 0.000031314550846568314439 |  201

因此,基本上,查询表中的所有内容并获得每个国家/地区的排名。我知道partition by在这里可能会有所帮助,但是我不知道它在上面的查询中适合什么位置,因为用ORDER BY替换它会得到有趣的结果。

这是为了创建一个API,该API每年显示特定国家/地区的每个排放数据,以及这些年份在人均排放量中的排名。

1 个答案:

答案 0 :(得分:0)

首先,您可以将rank()简化为:

RANK() OVER (ORDER BY COALESCE(emissions / NULLIF(population, 0), 0) DESC) as rank

如果您只想获得排名一年,则可以执行以下操作:

(sum(case when cp.year = 2010 then 1 else 0 end) over
 (order by case when cp.year = 2010 then coalesce(emissions / nullif(population, 0) end) -
 sum(case when cp.year = 2010 then 1 else 0 end) over (partition by cp.year, coalesce(emissions / nullif(population, 0)) +
 1
)

排名有些棘手,因为您必须考虑具有相同值的重复项。否则,您可以执行以下操作:

select . . .,
       max(case when year = 2010 then rank_year end) over (partition by countryid) as rank_2010
from (select . . .,
             rank() over (order by coalesce(emissions / nullif(population, 0), 0) desc) as rank_year

      from . . .
     ) t