postgresql合并string_agg函數(shù)的實例
1 有時候我們會需要將多條數(shù)據(jù)根據(jù)一些特別的字段做一些合并。比如下面這個查詢,正常會查詢出3條數(shù)據(jù),但是我們會希望根據(jù)create_by 分成兩列顯示

2 這時候需要用到string_agg函數(shù),先通過group by分組,在進(jìn)行合并,當(dāng)然查詢結(jié)果需要滿足group by的限制;sql語句:
select create_by,string_agg(videoname,',') as videonames from w008_video_addr_info where id in (4248,538,546) group by create_by
查詢結(jié)果:

3 復(fù)雜一些的應(yīng)用場景(子查詢):
下面的語句是我用來查詢一個學(xué)生在什么時間看了哪些視頻:
select sa.id, info.nickname, (select string_agg(v.videoname,',') from w008_school_assign_video sv join w008_video_addr_info v on sv.videoaddrinfo =v.id where sv.schoolassignment=sa.id and v.is_removed=0 and sv.is_removed=0 group by v.is_removed) as videos, (select string_agg(to_char(sv.create_date, 'MM-DD HH24:MI'),',') from w008_school_assign_video sv join w008_video_addr_info v on sv.videoaddrinfo =v.id where sv.schoolassignment=sa.id and v.is_removed=0 and sv.is_removed=0 group by v.is_removed) as viewtime from w008_school_assignment sa join w008_user_business_info info on sa.userlongid=info.id where sa.shchoolworkid=2514505674916356
結(jié)果:

當(dāng)然,string_agg(field,'分隔符');分隔符可以填寫其他任意的字符,方便后期處理即可;
補充:PostgreSql 聚合函數(shù)string_agg與array_agg,類似mysql中g(shù)roup_concat
string_agg,array_agg 這兩個函數(shù)的功能大同小異,只不過合并數(shù)據(jù)的類型不同。
https://www.postgresql.org/docs/9.6/static/functions-aggregate.html
array_agg(expression)
把表達(dá)式變成一個數(shù)組 一般配合 array_to_string() 函數(shù)使用
string_agg(expression, delimiter)
直接把一個表達(dá)式變成字符串
案例:
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:
select deptno, string_agg(ename, ',') from jinbo.employee group by deptno; deptno | string_agg --------+-------------- 20 | JONES 30 | ALLEN,MARTIN
方法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條件的基礎(chǔ)上,按ename 倒敘合并
select deptno, string_agg(ename, ',' order by ename desc) from jinbo.employee group by deptno; deptno | string_agg --------+-------------- 20 | JONES 30 | MARTIN,ALLEN
按數(shù)組格式輸出使用 array_agg
select deptno, array_agg(ename) from jinbo.employee group by deptno;
deptno | array_agg
--------+----------------
20 | {JONES}
30 | {ALLEN,MARTIN}
array_agg 去重元素,例如查詢所有的部門
select array_agg(distinct deptno) from jinbo.employee;
array_agg
-----------
{20,30}
(1 row)
#不僅可以去重,還可以排序
select array_agg(distinct deptno order by deptno desc) from jinbo.employee;
array_agg
-----------
{30,20}
(1 row)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
postgresql synchronous_commit參數(shù)的用法介紹
這篇文章主要介紹了postgresql synchronous_commit參數(shù)的用法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
postgresql 中position函數(shù)的性能詳解
這篇文章主要介紹了postgresql 中position函數(shù)的性能詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
如何將excel表格數(shù)據(jù)導(dǎo)入postgresql數(shù)據(jù)庫
這篇文章主要介紹了如何將excel表格數(shù)據(jù)導(dǎo)入postgresql數(shù)據(jù)庫,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
詳解PostgreSQL?14.4安裝使用及一些安裝的異常問題
這篇文章主要介紹了PostgreSQL?14.4的安裝以及使用以及一些安裝的異常,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
深入解讀PostgreSQL中的序列及其相關(guān)函數(shù)的用法
這篇文章主要介紹了PostgreSQL中的序列及其相關(guān)函數(shù)的用法,包括序列的更新和刪除等重要知識,需要的朋友可以參考下2016-01-01
解決postgresql 數(shù)字轉(zhuǎn)換成字符串前面會多出一個空格的問題
這篇文章主要介紹了解決postgresql 數(shù)字轉(zhuǎn)換成字符串前面會多出一個空格的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
PostgreSQL常用字符串函數(shù)與示例說明小結(jié)
文章介紹了PostgreSQL中常用字符串函數(shù)的使用方法,包括空值處理、字符串位置查詢、長度計算、大小寫轉(zhuǎn)換、去除空格、連接、替換、匹配、拆分和截取等操作,感興趣的朋友跟隨小編一起看看吧2024-11-11

