我有一个有排放和人口的数据库。我使用以下查询来获得人均排放量和每个国家的排名:
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_capita
和WHERE 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,每年显示特定国家/地区的每个排放数据,以及它们在所述年份的per_capita中的排名。
首先,您可以将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