码迷,mamicode.com
首页 > 其他好文 > 详细

postgres —— 分组集

时间:2019-12-17 00:11:31      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:msu   rollup   建表   bsp   filter   https   sel   text   class   

创建表

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

postgres —— 分组集

标签:msu   rollup   建表   bsp   filter   https   sel   text   class   

原文地址:https://www.cnblogs.com/lemos/p/12052086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!