聚合函數 postgre的string_agg、array_agg 和Oracle的WM_CONCAT、LISTAGG 和 Mysql的GROUP_CONCAT

 

一、PostgreSQL

函數說明

array_agg(expression)
把表達式變成一個數組 一般配合 array_to_string() 函數使用

string_agg(expression, delimiter)
直接把一個表達式變成字符串

使用案例 

select student, string_agg(score, ',' order by questionord) from table_score group by student;
select array_agg(distinct class) from table_student;


select array_agg(distinct class order by class desc) from table_student;

使用注意

array_agg 直接使用字符,需要加入類型轉換。否則會報錯。

array_agg('-'::VARCHAR order by ord)

 

參考:

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

 

 

二、Oracle

使用案例

select to_char(WMSYS.WM_CONCAT(score)) from table_score group by student;

 

select LISTAGG(SCORE, ',') WITHIN GROUP (ORDER BY QUESTIONORD) from table_score;

 

三、MySQL

使用案例

select CONVERT (GROUP_CONCAT(SCORE order by score desc separator ';') USING utf8) from table_score,

參考:

https://www.cnblogs.com/bigbigbigo/p/10952895.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章