3

假设我有这张桌子:

ID|Col1|Col2|Col3

1|Text that has 4000 chars|Text2 that has 4000 chars|Text3 that has 4000 chars

2|Text4 that has 4000 chars|Text5 that has 4000 chars|Text6 that has 4000 chars

3|Text7 that has 4000 chars|Text8 that has 4000 chars|Text9 that has 4000 chars

我正在使用 listagg 像这样:

SELECT id,
       listagg(col1||col2||col3, ',') within group (order by id)
FROM table;

我遇到了错误:

ORA-01489: result of string concatenation is too long

经过研究,我发现使用 xmlagg 可以解决问题(链接),但后来意识到真正的问题在于 col1、col2 和 col3 的串联,因为它仅限于 4000 个字符,所以执行 xmlagg 仍然会返回相同的错误.

有没有人想出这个?还是没有解决方法?(链接

更新:

为了清楚起见,我更新了表格上的示例值(供 Kumar 先生理解),我的预期输出应该是这样的:

ID | Agg
1 | Text that has 4000 charsText2 that has 4000 charsText3 that has 4000 chars
2 | Text4 that has 4000 charsText5 that has 4000 charsText6 that has 4000 chars
3 | Text7 that has 4000 charsText8 that has 4000 charsText9 that has 4000 chars

这显然行不通。

4

2 回答 2

1

我终于让它工作了。我所做的是在连接之前聚合列,按照 Kumar 先生的建议将其分组,然后再次聚合它们以修复排序。就是这样:

WITH agg_tbl AS
(
SELECT id,
       rtrim(xmlagg(xmlelement(e,col1,',').extract('//text()').GetClobVal(),',')||rtrim(xmlagg(xmlelement(e,col1,',').extract('//text()').GetClobVal(),',')||rtrim(xmlagg(xmlelement(e,col1,',').extract('//text()').GetClobVal(),',') long_text
FROM table
GROUP BY col1
)
SELECT rtrim(xmlagg(xmlelement(e,long_text,chr(13)).extract('//text()').GetClobVal(),',') agg_value
FROM agg_tbl;

所以代替这个:

agg_value
Text that has 4000 charsText4 that has 4000 charsText7 that has 4000 chars
Text2 that has 4000 charsText5 that has 4000 charsText8 that has 4000 chars
Text3 that has 4000 charsText6 that has 4000 charsText9 that has 4000 chars

我现在得到了我想要的结果:

agg_value
Text that has 4000 charsText2 that has 4000 charsText3 that has 4000 chars
Text4 that has 4000 charsText5 that has 4000 charsText6 that has 4000 chars
Text7 that has 4000 charsText8 that has 4000 charsText9 that has 4000 chars
于 2015-12-03T03:12:54.220 回答
1

由于 Oracle前段时间引入了SQL Semantics 和 LOB,因此您可以做的更简单。

SELECT ID, TO_CLOB(col1) || col2 || col3 AS very_long_text
FROM TABLE;

运算符的第一个元素||必须是 a CLOB,然后它才能工作。

于 2015-12-02T09:35:53.757 回答