postgresql合并string_agg函數(shù)的實(shí)例
1 有時(shí)候我們會(huì)需要將多條數(shù)據(jù)根據(jù)一些特別的字段做一些合并。比如下面這個(gè)查詢,正常會(huì)查詢出3條數(shù)據(jù),但是我們會(huì)希望根據(jù)create_by 分成兩列顯示
2 這時(shí)候需要用到string_agg函數(shù),先通過(guò)group by分組,在進(jìn)行合并,當(dāng)然查詢結(jié)果需要滿足group by的限制;sql語(yǔ)句:
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)用場(chǎng)景(子查詢):
下面的語(yǔ)句是我用來(lái)查詢一個(gè)學(xué)生在什么時(shí)間看了哪些視頻:
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,'分隔符');分隔符可以填寫(xiě)其他任意的字符,方便后期處理即可;
補(bǔ)充:PostgreSql 聚合函數(shù)string_agg與array_agg,類似mysql中g(shù)roup_concat
string_agg,array_agg 這兩個(gè)函數(shù)的功能大同小異,只不過(guò)合并數(shù)據(jù)的類型不同。
https://www.postgresql.org/docs/9.6/static/functions-aggregate.html
array_agg(expression)
把表達(dá)式變成一個(gè)數(shù)組 一般配合 array_to_string() 函數(shù)使用
string_agg(expression, delimiter)
直接把一個(gè)表達(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)
查詢同一個(gè)部門下的員工且合并起來(lái)
方法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)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- Postgresql去重函數(shù)distinct的用法說(shuō)明
- PostgreSQL 定義返回表函數(shù)的操作
- PostgreSQL的generate_series()函數(shù)的用法說(shuō)明
- PostgreSQL批量修改函數(shù)擁有者的操作
- PostgreSQL數(shù)據(jù)類型格式化函數(shù)操作
- 在postgresql數(shù)據(jù)庫(kù)中判斷是否是數(shù)字和日期時(shí)間格式函數(shù)操作
- Postgresql自定義函數(shù)詳解
- postgresql 循環(huán)函數(shù)的簡(jiǎn)單實(shí)現(xiàn)操作
相關(guān)文章
PostgreSQL與MySQL優(yōu)劣勢(shì)比較淺談
這篇文章主要詳細(xì)介紹了PostgreSQL與MySQL有哪些優(yōu)劣勢(shì),文中介紹的非常詳細(xì),對(duì)于學(xué)習(xí)有一定的幫助,感興趣的小伙伴可以參考一下2023-04-04使用PostgreSQL數(shù)據(jù)庫(kù)建立用戶畫(huà)像系統(tǒng)的方法
這篇文章主要介紹了使用PostgreSQL數(shù)據(jù)庫(kù)建立用戶畫(huà)像系統(tǒng),下面使用一個(gè)具體的例子來(lái)說(shuō)明如何使用PostgreSQL的json數(shù)據(jù)類型來(lái)建立用戶標(biāo)簽數(shù)據(jù),需要的朋友可以參考下2022-10-10PostgreSQL物理備份恢復(fù)之 pg_rman的用法說(shuō)明
這篇文章主要介紹了PostgreSQL物理備份恢復(fù)之 pg_rman的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02pg中replace和translate的用法說(shuō)明(數(shù)據(jù)少的中文排序)
這篇文章主要介紹了pg中replace和translate的用法說(shuō)明(數(shù)據(jù)少的中文排序),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL數(shù)據(jù)庫(kù)中如何保證LIKE語(yǔ)句的效率(推薦)
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫(kù)中如何保證LIKE語(yǔ)句的效率,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03postgresql 實(shí)現(xiàn)啟動(dòng)、狀態(tài)查看、關(guān)閉
這篇文章主要介紹了postgresql 實(shí)現(xiàn)啟動(dòng)、狀態(tài)查看、關(guān)閉的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Postgresql?REGEXP開(kāi)頭的正則函數(shù)用法圖文詳解
正則表達(dá)式是指一個(gè)用來(lái)描述或者匹配一系列符合某個(gè)句法規(guī)則的字符串的單個(gè)字符串,下面這篇文章主要給大家介紹了關(guān)于Postgresql?REGEXP開(kāi)頭的正則函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2024-02-02