PostgreSQL: 如何连接 " group by " 结果集的行?
来源:互联网 发布:linux php集成环境包 编辑:程序博客网 时间:2024/04/29 10:58
如何连接 " group by" 结果集的行? 有点像行列转换,但又不完全是,
描述颇为费劲,举例如下:
假如一张表有以下数据:
中国 台北
中国 香港
中国 上海
日本 东京
日本 大阪
要求得到如下结果集:
中国 台北,香港,上海
日本 东京,大阪
此文介绍几种实现以上要求的方法: PostgreSQL 版本为 9.0
--创建初始表并插入数据
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_aggList 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)
三 方法三:自定义聚集函数
--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
- PostgreSQL: 如何连接 " group by " 结果集的行?
- group by如何保留count为0的结果?
- postgreSQL GROUP BY
- 对group by 选取结果的排序
- 使用JAVA连接AccDB文件,GROUP BY 出现多余结果的问题
- MySQL group by后的结果将每组某VARCHAR字段字符串连接起来
- 如何将两个不同分组(group by)的结果拼成逗号隔开的字符串
- 含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- 含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- MySql含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- group by后的汇总结果,如何去和总和做百分比
- SQL结果统计 GROUP BY
- GROUP BY的另种使用方法:将结果集一列的内容作为一个单元格显示
- 对group by的结果集进行分组过滤 where 与 having 用法的区别
- hql不能在distinct,group by结果集上使用count的问题,报语法错误
- 【SQL心得】:在含有GROUP BY的SELECT语句中如何显示COUNT()为0的结果
- MySQL数据库——对查询结果的基本操作(GROUP BY、HAVING、ORDER BY、LIMIT)
- 在Pandas中,如何根据Group By 结果计算 Row Number
- ios网络学习------2 用非代理方法实现同步post请求
- ListView点击列排序的实现
- TLV编码
- j2se--Socket通信
- 自己实现一个SQL解析引擎
- PostgreSQL: 如何连接 " group by " 结果集的行?
- 安卓接入第三方平台时快速加入第三方Logo
- oracle dblink 详解
- Dll学习(二)__declspec用法详解
- 如何批量添加destoon的文章
- 华为外网可以访问OJ平台了
- 如何按列 Visual C# 排序列表视图控件
- struts1之global-forwards
- java 反射详解