string_agg 与array_agg

news/2024/7/2 1:22:09

string_agg

实例1

  • 数据
imos=# select res_id, res_name from test;
 res_id |    res_name
--------+----------------
      1 | Root
  10001 | EC_PAG
  10002 | EC_PAG_GUOBIAO
(3 rows)
  • 普通string_agg
imos=# select string_agg(res_name,';') from test;
         string_agg
----------------------------
 Root;EC_PAG;EC_PAG_GUOBIAO
(1 row)
  • 带有order by 的string_agg
imos=# select string_agg(res_name,';' order by res_name ) from test;
         string_agg
----------------------------
 EC_PAG;EC_PAG_GUOBIAO;Root
(1 row)

实例2

  • 数据
postgres=# create table test(id int,name varchar(20));
CREATE TABLE
postgres=# insert into test values(1,'a');
INSERT 0 1
postgres=# insert into test values(1,'b');
INSERT 0 1
postgres=# insert into test values(1,'c');
INSERT 0 1
postgres=# insert into test values(2,'d');
INSERT 0 1
postgres=# insert into test values(2,'e');
INSERT 0 1
postgres=# select * from test;
 id | name
----+------
  1 | a
  1 | b
  1 | c
  2 | d
  2 | e
(5 rows)

  • 不分组

postgres=# select string_agg(name,',') from test;
 string_agg
------------
 a,b,c,d,e
(1 row)
  • 分组
postgres=# select id ,  string_agg(name,',') from test group by id;
 id | string_agg
----+------------
  2 | d,e
  1 | a,b,c
(2 rows)

array_agg

imos=# select array_agg(res_name) from test;
ERROR:  could not find array type for data type imos_name
imos=#
imos=#
imos=#
imos=# select array_agg(res_name::varchar) from test;
          array_agg
------------------------------
 {Root,EC_PAG,EC_PAG_GUOBIAO}
(1 row)

imos=#
imos=# select array_agg(res_name::varchar order by res_name ) from test;
          array_agg
------------------------------
 {EC_PAG,EC_PAG_GUOBIAO,Root}
(1 row)


http://www.niftyadmin.cn/n/656978.html

相关文章

postgres 行列转换

行转列 imos# create table test_split_to_table(id int, name_list varchar); CREATE TABLE imos# insert into test_split_to_table values(1,a;b;c),(2,d;e;f); INSERT 0 2 imos# imos# select * from test_split_to_table ;id | name_list ---------------1 | a;b;c2 | d;…

weblogic 连接mysql_weblogic 9.2下的domain配置mysql连接池步骤:

本文介绍在weblogic 9.2下的domain配置mysql连接池步骤:1.在weblogic目录下的server/lib下添加mysql驱动mysql-connector-java-5.1.5-bin.jar2.在weblogic目录下的common/bin下找到commEnv.cmd打开找到set weblogic_classpath 后边加上mysql驱动的路径例&#xff1a…

error2019-01-17 宏STDOUT_FILENO

STDOUT_FILENO定义在<unistd.h> EXIT_SUCCESS <stdlib.h> 1.fatal error: sys/capability.h: No such file or directory #include <sys/capability.h> 解决办法:sudo apt-get install libcap-dev 2.fatal error: sys/acl.h: No such file or directory #inc…

pg_dump导出数据

导出结构 /home/postgres/pgsql/bin/pg_dump -Upostgres -d if -s --no-tablespaces -f if_create_obj.sql导出数据 copy形式 /home/postgres/pgsql/bin/pg_dump -Upostgres -d if -a --no-tablespaces -f if_init.sqlinsert语句形式&#xff08;通过添加–inserts选项…

dbvisua连接mysql8_VS2019连接mysql8.0数据库的教程图文详解

VS2019连接mysql8.0数据库的教程图文详解2020-06-17 14:45:591.首先准备好VS2019以及mysql数据库&#xff0c;两者都可以去官网下载&#xff0c;我们直接描述连接过程。2.连接&#xff1a;第一步&#xff1a;打开mysql的安装目录&#xff0c;我本地的安装目录如下&#xff1a;(…

loj #6235. 区间素数个数 min_12.5筛

\(\color{#0066ff}{ 题目描述 }\) 求 \(1\sim n\) 之间素数个数。 \(\color{#0066ff}{输入格式}\) 一行一个数 n 。 \(\color{#0066ff}{输出格式}\) 一行一个数&#xff0c;表示答案。 \(\color{#0066ff}{输入样例}\) 10 \(\color{#0066ff}{输出样例}\) 4 \(\color{#0066ff}{数…

babun(一种window下的命令行工具)安装篇

1、下载&#xff1a; 官网&#xff1a;http://babun.github.io/ 站内下载地址&#xff08;共计4个文件&#xff0c;另外两个还没有显示出来&#xff09;&#xff1a; http://download.csdn.net/detail/mengyoufengyu/9681075 http://download.csdn.net/detail/mengyoufeng…

mysql二进制日志文件恢复数据库_mysql二进制日志文件恢复数据库

二进制日志的文件的作用mysql二进制日志文件用来记录所有用户对数据库操作&#xff0c;即记录用户对数据库操作的sql语句。如果有此文件&#xff0c;当数据库发生意外时&#xff0c;可以通过此文件查看到用户在此文件记录的时间段内用户所做的操作&#xff0c;再和数据库备份配…