postgresql合并string_agg函數(shù)的實例
1 有時候我們會需要將多條數(shù)據(jù)根據(jù)一些特別的字段做一些合并。比如下面這個查詢,正常會查詢出3條數(shù)據(jù),但是我們會希望根據(jù)create_by 分成兩列顯示
2 這時候需要用到string_agg函數(shù),先通過group by分組,在進行合并,當然查詢結果需要滿足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
查詢結果:
3 復雜一些的應用場景(子查詢):
下面的語句是我用來查詢一個學生在什么時間看了哪些視頻:
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
結果:
當然,string_agg(field,'分隔符');分隔符可以填寫其他任意的字符,方便后期處理即可;
補充:PostgreSql 聚合函數(shù)string_agg與array_agg,類似mysql中group_concat
string_agg,array_agg 這兩個函數(shù)的功能大同小異,只不過合并數(shù)據(jù)的類型不同。
https://www.postgresql.org/docs/9.6/static/functions-aggregate.html
array_agg(expression)
把表達式變成一個數(shù)組 一般配合 array_to_string() 函數(shù)使用
string_agg(expression, delimiter)
直接把一個表達式變成字符串
案例:
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條件的基礎上,按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)
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
使用PostgreSQL數(shù)據(jù)庫建立用戶畫像系統(tǒng)的方法
這篇文章主要介紹了使用PostgreSQL數(shù)據(jù)庫建立用戶畫像系統(tǒng),下面使用一個具體的例子來說明如何使用PostgreSQL的json數(shù)據(jù)類型來建立用戶標簽數(shù)據(jù),需要的朋友可以參考下2022-10-10PostgreSQL物理備份恢復之 pg_rman的用法說明
這篇文章主要介紹了PostgreSQL物理備份恢復之 pg_rman的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02pg中replace和translate的用法說明(數(shù)據(jù)少的中文排序)
這篇文章主要介紹了pg中replace和translate的用法說明(數(shù)據(jù)少的中文排序),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL數(shù)據(jù)庫中如何保證LIKE語句的效率(推薦)
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫中如何保證LIKE語句的效率,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03postgresql 實現(xiàn)啟動、狀態(tài)查看、關閉
這篇文章主要介紹了postgresql 實現(xiàn)啟動、狀態(tài)查看、關閉的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Postgresql?REGEXP開頭的正則函數(shù)用法圖文詳解
正則表達式是指一個用來描述或者匹配一系列符合某個句法規(guī)則的字符串的單個字符串,下面這篇文章主要給大家介紹了關于Postgresql?REGEXP開頭的正則函數(shù)用法的相關資料,需要的朋友可以參考下2024-02-02