在oracle中可以使用listagg()函数实现列转行的操作,将多列记录聚合为一条记录。
官方文档参考:
参考链接
官方示例:
示例1:
The following single-set aggregate example lists all of the employees in Department 30 in the hr.employees table, ordered by hire date and last name:
下述语句会在员工表中找出部门编号为30的员工,并且按照他们的雇佣日期和姓进行排序(默认是升序)
SELECT LISTAGG(last_name, '; ')
WITHIN GROUP (ORDER BY hire_date, last_name) "Emp_list",
MIN(hire_date) "Earliest"
FROM employees
WHERE department_id = 30;
Emp_list Earliest
------------------------------------------------------------ ---------
Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares 07-DEC-02
示例2:
The following group-set aggregate example lists, for each department ID in the hr.employees table, the employees in that department in order of their hire date:
下述语句表示:
列出各个部门的员工,并将这些员工按照他们的雇佣日期进行排序(默认为升序)
SELECT department_id "Dept.",
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date) "Employees"
FROM employees
GROUP BY department_id
ORDER BY department_id;
Dept. Employees
------ ------------------------------------------------------------
10 Whalen
20 Hartstein; Fay
30 Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares
40 Mavris
50 Kaufling; Ladwig; Rajs; Sarchand; Bell; Mallin; Weiss; Davie
s; Marlow; Bull; Everett; Fripp; Chung; Nayer; Dilly; Bissot
; Vollman; Stiles; Atkinson; Taylor; Seo; Fleaur; Matos; Pat
el; Walsh; Feeney; Dellinger; McCain; Vargas; Gates; Rogers;
Mikkilineni; Landry; Cabrio; Jones; Olson; OConnell; Sulliv
an; Mourgos; Gee; Perkins; Grant; Geoni; Philtanker; Markle
60 Austin; Hunold; Pataballa; Lorentz; Ernst
70 Baer
. . .
示例3:
关于最大长度的问题:
The maximum length of the return data type depends on the value of the MAX_STRING_SIZE initialization parameter. If MAX_STRING_SIZE = EXTENDED, then the maximum length is 32767 bytes for the VARCHAR2 and RAW data types. If MAX_STRING_SIZE = STANDARD, then the maximum length is 4000 bytes for the VARCHAR2 data type and 2000 bytes for the RAW data type. A final delimiter is not included when determining if the return value fits in the return data type.
返回数据的最大长度取决于 MAX_STRING_SIZE 初始化参数的值。
如果MAX_STRING_SIZE = EXTENDED,那么最大长度就是VARCHAR2 :32767 bytes,RAW data :32767 bytes;
如果MAX_STRING_SIZE = STANDARD,那么最大长度为:
VARCHAR2:4000 bytes,RAW data:2000 bytes 。
The following example is identical to the previous example, except it contains the ON OVERFLOW TRUNCATE clause. For the purpose of this example, assume that the maximum length of the return value is an artificially small number of 200 bytes. Because the list of employees for department 50 exceeds 200 bytes, the list is truncated and appended with a final delimiter '; ‘, the specified truncation indicator ‘…’, and the number of truncated values ‘(23)’.
下述示例与前面的例子差不多,但是含有ON OVERFLOW TRUNCATE 条件。
假如返回的最大长度是200 bytes,因为部门50的员工拼接起来会大于这个长度,所以这个值会被截断,并且以’; '结尾,用 ‘…‘表明这个数据是被截断的,被截断的数量为’(23)’.
SELECT department_id "Dept.",
LISTAGG(last_name, '; ' ON OVERFLOW TRUNCATE '...')
WITHIN GROUP (ORDER BY hire_date) "Employees"
FROM employees
GROUP BY department_id
ORDER BY department_id;
Dept. Employees
------ ------------------------------------------------------------
10 Whalen
20 Hartstein; Fay
30 Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares
40 Mavris
50 Kaufling; Ladwig; Rajs; Sarchand; Bell; Mallin; Weiss; Davie
s; Marlow; Bull; Everett; Fripp; Chung; Nayer; Dilly; Bissot
; Vollman; Stiles; Atkinson; Taylor; Seo; Fleaur; ... (23)
70 Baer
. . .
其他例子参考官网。
在GBASE中与之功能相似的函数为GROUP_CONCAT()函数。
函数基本信息:
具体内容参考官方文档或移步另一篇文章下载手册(手册825-826页描述了该函数的基本信息):
官方文档链接
一些关于GBASE的文档
假如我们有一个机构表,机构分为两个等级,我们希望找出每个一级机构下面的二级机构,那么使用此函数可以实现该操作。
示例:
SELECT UP_ORG,GROUP_CONCAT(ORG) FROM ORG_TABLE
GROUP BY UP_ORG
-----------------------------------
RESULT:
UP_ORG GROUP_CONCAT(ORG)
DEP1 SUBDEP11,SUBDEP12,SUBDEP13
DEP2 SUBDEP21,SUBDEP22
DEP3 SUBDEP31
当我们不指定分隔符的时候,默认使用逗号进行分隔
示例2:
加上条件筛选,并且改变分隔符为’/’
SELECT UP_ORG,GROUP_CONCAT(ORG SEPARATOR '/' ) FROM ORG_TABLE
WHERE DATA_DT = '20231231'
GROUP BY UP_ORG
-----------------------------------
RESULT:
UP_ORG GROUP_CONCAT(ORG SEPARATOR '/' )
DEP1 SUBDEP11/SUBDEP12/SUBDEP13
DEP2 SUBDEP21/SUBDEP22
DEP3 SUBDEP31
示例3:
排序:
按照机构名字进行逆序排序,不指定的话,默认为升序
SELECT UP_ORG,GROUP_CONCAT(ORG order BY ORG DESC) FROM ORG_TABLE
GROUP BY UP_ORG
-----------------------------------
RESULT:
UP_ORG GROUP_CONCAT(ORG order BY ORG DESC)
DEP1 SUBDEP13,SUBDEP12,SUBDEP11
DEP2 SUBDEP22,SUBDEP21
DEP3 SUBDEP31
示例4:
指定拼接的最大个数
这里指定了拼接的最大数量为2,那么最多只会拼接两个值
SELECT UP_ORG,GROUP_CONCAT(ORG TOPN 2) FROM ORG_TABLE
GROUP BY UP_ORG
-----------------------------------
RESULT:
UP_ORG GROUP_CONCAT(ORG TOPN 2)
DEP1 SUBDEP11,SUBDEP12
DEP2 SUBDEP21,SUBDEP22
DEP3 SUBDEP31
示例5:
去重(ORACLE也有此用法)
加上DISTINCT 就只会保留一个唯一值
SELECT UP_ORG,GROUP_CONCAT(DISTINCT ORG ) FROM ORG_TABLE
GROUP BY UP_ORG
-----------------------------------
RESULT:
UP_ORG GROUP_CONCAT(DISTINCT ORG )
DEP1 SUBDEP11,SUBDEP12,SUBDEP13
DEP2 SUBDEP21,SUBDEP22
DEP3 SUBDEP31
同样GROUP_CONCAT()函数也对最大长度做了限制,具体参考上述参数说明。
另外值得注意的是:GBASE似乎不支持DISTINCT 与ORDER BY 一起使用(最起码8A不支持)
如:
SELECT UP_ORG,GROUP_CONCAT(DISTINCT ORG ORDER BY ORG DESC ) FROM ORG_TABLE
group by UP_ORG
-----------------------------------
这种写法是不被允许的。
在此函数中使用多个参数的时候注意参数的顺序:
SELECT
UP_ORG,
GROUP_CONCAT(ORG ORDER BY ORG DESC TOPN 6 SEPARATOR '/') FROM ORG_TABLE
WHERE ......
group by UP_ORG
以上。