聚合函数 listagg (超出长度限制时xmlagg)
表&数据
CREATE TABLE peoplebuy (people Varchar2(10),buy Varchar2(10),price NUMBER); INSERT INTO peoplebuy VALUES ('我','小猫',10); INSERT INTO peoplebuy VALUES ('我','小狗',200); INSERT INTO peoplebuy VALUES ('你','树',20);
原来的结果
SELECT * FROM PEOPLEBUY ORDER BY PEOPLE;
想要的结果
SELECT PEOPLE, LISTAGG(BUY, ',') WITHIN GROUP(ORDER BY BUY DESC) AS BUY, SUM(PRICE) TOTAL FROM PEOPLEBUY GROUP BY PEOPLE ORDER BY PEOPLE
用法
LISTAGG(measure_expr [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER query_partition_clause]
当连接的字符串过长时会出现以下错误:
解决方案:
XMLAGG https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm
XMLType https://docs.oracle.com/cd/B10501_01/appdev.920/a96616/arxml24.htm
RTRIM(XMLAGG(XMLELEMENT(e, t.signature_id, ',').extract('//text()')).getClobVal(),',') very_long_text
聚合元素,提取文本,获取clob,去空格