group_concat on postgresql
来源:互联网 发布:matable软件 编辑:程序博客网 时间:2024/04/29 22:04
CREATE TABLE produk(seq_id serial NOT NULL,name character varying(100) NOT NULL) ;INSERT INTO produk (seq_id, name) VALUES (1, 'beer');INSERT INTO produk (seq_id, name) VALUES (2, 'in');INSERT INTO produk (seq_id, name) VALUES (3, 'beer');INSERT INTO produk (seq_id, name) VALUES (4, 'can');INSERT INTO produk (seq_id, name) VALUES (6, 'goods');INSERT INTO produk (seq_id, name) VALUES (7, 'goods');INSERT INTO produk (seq_id, name) VALUES (8, 'can');INSERT INTO produk (seq_id, name) VALUES (5, 'goods');
MySQL:
select name, group_concat(cast(seq_id as char)) as id_of_duplicatesfrom produkgroup by nameorder by name;
PostgreSQL:
create aggregate array_accum (sfunc = array_append,basetype = anyelement,stype = anyarray,initcond = '{}');CREATE OR REPLACE FUNCTION _group_concat(text, text)RETURNS text AS $$SELECT CASEWHEN $2 IS NULL THEN $1WHEN $1 IS NULL THEN $2ELSE $1 operator(pg_catalog.||) ',' operator(pg_catalog.||) $2END$$ IMMUTABLE LANGUAGE SQL;CREATE AGGREGATE group_concat (BASETYPE = text,SFUNC = _group_concat,STYPE = text);
first approach:
select name, array_accum(seq_id)from produkgroup by nameselect name, array_to_string(array_accum(seq_id), ',')from produkgroup by name;
second approach (mysql-compatible approach):
select name, group_concat(seq_id) as id_of_duplicatesfrom produkgroup by nameorder by name;
MySQL GROUP_CONCAT with ordering:
select name, group_concat(cast(seq_id as char) order by seq_id) as id_of_duplicatesfrom produkgroup by nameorder by name;
PostgreSQL equivalent:
select name, group_concat(distinct seq_id) as id_of_duplicatesfrom produkgroup by nameorder by name;
using customized sort:
select name, group_concat(seq_id) as id_of_duplicatesfrom(select name, seq_idfrom produkorder by name, seq_id) as xgroup by name
http://s2.diffuse.it/blog/show/10-group_concat_on_postgresql
- group_concat on postgresql
- PostgreSQL中的group_concat使用
- postgresql如何实现group_concat功能
- PostGreSql 内部缺失GROUP_CONCAT 函数解决方案
- group_concat
- group_concat
- GROUP_CONCAT
- group_concat
- GROUP_CONCAT
- group_concat
- postgreSQL中的array_to_string函数和如何实现group_concat
- PostgreSQL on Ubuntu Linux
- uninstall postgresql on xp
- Install PostgreSQL on Ubuntu
- PostgreSQL installation on Ubuntu
- install postgresql on ubuntu
- PostgreSQL DISTINCT ON
- postgreSql on windows
- Windows普通程序与服务程序的初始特权(Privileges)
- webservice使用handler来增强web服务的功能
- WINCE平台安装sqlCE
- C++设置和获取环境变量
- 使用linuxmint系统的一些感受
- group_concat on postgresql
- JYJ_Block_Science and Technology
- 24c01~24c1024全系列EEPROM的读写程序
- Linux设备驱动程序开发流程示例
- Erlang 编程参考手册(第一部分)
- [转]修道小妖又出新教程啦~使用Apophysis制作3D花的新方法!
- Least common multiple(H1019)
- Erlang 编程手册(第二部分,包括匹配和模块)
- Android动画效果translate、scale、alpha、rotate