连接太久了

时间:2015-12-02 06:24:01

标签: sql oracle limit concat listagg

说我有这张桌子:

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可以做到这一点(link),但后来意识到真正的问题在于col1,col2和col3的串联,因为它只限于4000个字符,所以这样做xmlagg仍然会返回相同的错误。

有没有人想出这个呢?或者没有解决方法吗? (link

更新

我更新了表格上的示例值(为了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

显然不起作用。

2 个答案:

答案 0 :(得分:1)

你可以做得更简单,因为甲骨文不久前推出了SQL Semantics and LOBs

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

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

答案 1 :(得分:1)

我终于开始工作了。我所做的是在连接之前聚合列,按照库马尔先生的建议将其分组,然后再次聚合它们来修复排序。方法如下:

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