创建表

create table t_oil (
    region text,
    country text,
    year   text,
    production int,
    comsumption int
)

 

导入数据

copy t_oil from program 'curl https://cybertec-postgresql.com/secret/oil_ext.txt';

 

分组集的应用

-- region,country 4 + region 2 + 1
SELECT region, country, avg(production) from t_oil where country in ('USA', 'Canada', 'Iran', 'Oman') 
group by  rollup (region, country);

-- region,country 4 + region 2 + country 4 + 1
SELECT region, country, avg(production) from t_oil where country in ('USA', 'Canada', 'Iran', 'Oman') 
group by  cube (region, country);

-- same above
SELECT region, country, avg(production) from t_oil where country in ('USA', 'Canada', 'Iran', 'Oman') 
group by GROUPING SETS ((), region, country, (region, country));

 

部分聚集 filter

SELECT region, avg(production) as all,
avg(production) FILTER (where year < 1990) as old,
avg(production) FILTER (where year >= 1990) as new
from t_oil 
group by rollup(region);

  

233

相关文章:

  • 2022-12-23
  • 2021-12-12
  • 2021-05-12
  • 2022-02-14
  • 2021-08-10
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-08-03
  • 2021-11-09
  • 2022-12-23
相关资源
相似解决方案