PostgreSQL: 如何连接 " group by " 结果集的行?

如何连接 " group by" 结果集的行? 有点像行列转换,但又不完全是,
描述颇为费劲,举例如下:
    
假如一张表有以下数据:    
中国                 台北 
中国                 香港
中国                 上海 
日本                 东京 
日本                 大阪

要求得到如下结果集: 
中国    台北,香港,上海 
日本    东京,大阪

      此文介绍几种实现以上要求的方法: PostgreSQL 版本为 9.0
     
--创建初始表并插入数据

 skytf=> create table city (country character varying(64),city character varying(64));
CREATE TABLE

skytf=> insert into city values ('中国','台北');
INSERT 0 1
skytf=> insert into city values ('中国','香港');
INSERT 0 1
skytf=> insert into city values ('中国','上海');
INSERT 0 1
skytf=> insert into city values ('日本','东京');
INSERT 0 1
skytf=> insert into city values ('日本','大阪');
INSERT 0 1

skytf=> select * From city;
 country | city 
---------+------
 中国    | 台北
 中国    | 香港
 中国    | 上海
 日本    | 东京
 日本    | 大阪
(5 rows)

   


一 方法一:使用聚集函数 array_agg
--1.1 函数说明
Function name     array_agg
Argument Type(s)  any 
Return Type       array of the argument type 
Description       input values, including nulls, concatenated into an array

--1.2 测试函数

 skytf=> \df array_agg
                           List of functions
   Schema   |   Name    | Result data type | Argument data types | Type 
------------+-----------+------------------+---------------------+------
 pg_catalog | array_agg | anyarray         | anyelement          | agg
(1 row)

skytf=> select country,array_agg(city) from city group by country;
 country |    array_agg     
---------+------------------
 中国    | {台北,香港,上海}
 日本    | {东京,大阪}
(2 rows)    

     备注: 这正是我们需要的结果,返回的结果类型为数组。
          
二 方法二:使用聚集函数 string_agg   
--2.1函数说明        
Function name     string_agg(expression, delimiter) 
Argument Type(s)  text, text  
Return Type       text 
Description       input values concatenated into a string, separated by delimiter 

--2.2 测试函数

 skytf=> select country,string_agg(city,',') from city group by country;
 country |   string_agg   
---------+----------------
 中国    | 台北,香港,上海
 日本    | 东京,大阪
(2 rows)

skytf=> select country,string_agg(city,',') from city where country='中国' group by country;
 country |   string_agg   
---------+----------------
 中国    | 台北,香港,上海
(1 row)

       备注;string_agg 函数返回结果为 text 类型。
    

三 方法三:自定义聚集函数
--3.1 创建聚集函数
skytf=> CREATE AGGREGATE array_accum(anyelement) (
skytf(>     SFUNC = array_append,
skytf(>     STYPE = anyarray,
skytf(>     INITCOND = '{}'
skytf(> );
CREATE AGGREGATE


--3.2 测试函数
skytf=> select country,array_accum(city) from city where country='中国' group by country;
 country |   array_accum    
---------+------------------
 中国    | {台北,香港,上海}
(1 row)

 备注:同样得到了预期的结果,返回的类型为数组。
 
 
四 参考
http://www.postgresql.org/docs/9.0/static/functions-aggregate.html
http://www.postgresql.org/docs/9.0/static/sql-createaggregate.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值