PostgreSql implements the aggregation operation of mysql group ﹣ concat

Keywords: MySQL PostgreSQL

Group concat is an aggregation function in mysql, but PostgreSql does not have this function, but it can be implemented with string AGG function


Source: https://blog.csdn.net/u011944141/article/details/78902678


array_agg(expression)
To change an expression into an array is generally used in conjunction with the array'to'string() function
  • 1
  • 2
string_agg(expression, delimiter)
Turn an expression directly into a string
  • 1
  • 2

Case study:

create table(empno smallint, ename varchar(20), job varchar(20), mgr smallint, hiredate date, sal bigint, comm bigint, deptno smallint);

insert into jinbo.employee(empno,ename,job, mgr, hiredate, sal, comm, deptno) values (7499, 'ALLEN', 'SALEMAN', 7698, '2014-11-12', 16000, 300, 30);

insert into jinbo.employee(empno,ename,job, mgr, hiredate, sal, comm, deptno) values (7499, 'ALLEN', 'SALEMAN', 7698, '2014-11-12', 16000, 300, 30);

insert into jinbo.employee(empno,ename,job, mgr, hiredate, sal, comm, deptno) values (7654, 'MARTIN', 'SALEMAN', 7698, '2016-09-12', 12000, 1400, 30);

select * from jinbo.employee;
 empno | ename  |   job   | mgr  |  hiredate  |  sal  | comm | deptno 
-------+--------+---------+------+------------+-------+------+--------
  7499 | ALLEN  | SALEMAN | 7698 | 2014-11-12 | 16000 |  300 |     30
  7566 | JONES  | MANAGER | 7839 | 2015-12-12 | 32000 |    0 |     20
  7654 | MARTIN | SALEMAN | 7698 | 2016-09-12 | 12000 | 1400 |     30
(3 rows)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

1. Query and consolidate employees in the same department

Method 1:
select deptno, string_agg(ename, ',') from jinbo.employee group by deptno;

 deptno |  string_agg  
--------+--------------
     20 | JONES
     30 | ALLEN,MARTIN

//Method 2:
select deptno, array_to_string(array_agg(ename),',') from jinbo.employee group by deptno;
 deptno | array_to_string 
--------+-----------------
     20 | JONES
     30 | ALLEN,MARTIN
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2. On the basis of 1 condition, reverse merge by ename

select deptno, string_agg(ename, ',' order by ename desc) from jinbo.employee group by deptno;
 deptno |  string_agg  
--------+--------------
     20 | JONES
     30 | MARTIN,ALLEN
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. Output in array format using array

select deptno, array_agg(ename) from jinbo.employee group by deptno;
 deptno |   array_agg    
--------+----------------
     20 | {JONES}
     30 | {ALLEN,MARTIN}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. Array ABCD de duplication elements, such as querying all departments

select array_agg(distinct deptno) from jinbo.employee;
array_agg 
-----------
 {20,30}
(1 row)

#Not only can they be de duplicated, but they can also be sorted

select array_agg(distinct deptno order by deptno desc) from jinbo.employee;
 array_agg 
-----------
 {30,20}
(1 row)

Posted by gizmola on Thu, 02 Apr 2020 18:11:08 -0700