PG聚合操作中的filter子句是ANSI SQL标准中的关键字,并不是PG的专用SQL关键字。如果我们想了解中国、美国、日本、法国、德国、加拿大从1960~2018年中每隔十年的GDP平均值情况,我们可能会写出着这样的SQL,
select country_name, sum(case when year>=1960 and year<1970 then gdp else null end) as "1960~1969",
sum(case when year>=1970 and year<1980 then gdp else null end) as "1970~1979",
sum(case when year>=1980 and year<1990 then gdp else null end) as "1980~1989",
sum(case when year>=1990 and year<2000 then gdp else null end) as "1990~1999",
sum(case when year>=2000 then gdp else null end) as "2000~至今"
from country_gdp_year_final
where country_code in('CHN','JPN','U