将行连接到CLOB

时间:2013-04-16 15:16:26

标签: sql oracle

关于这个问题有很多类似的问题,但我找不到任何解决方案,考虑到任何最终结果对varchar2来说太大的问题。

所以我要做的就是改变这个:

Column1 | Column2
-------- --------
1         Hello
1         world,
1         please help
2         Thanks
2         world,
2         you're the best.

进入这个:

Column1 | Column2
-------- --------
1         Hello world, please help
2         Thanks world, you're the best.

我的特殊问题是,在某些情况下,新的连接值超过4000个字符,因此我无法按照我的意愿使用LISTAGG。我对解决方案特别感兴趣,无需编写函数,但要么会这样做。

4 个答案:

答案 0 :(得分:2)

正如de.hh.holger已经指出的那样,LISTAGG WITH CLOB? STRING AGGREGATION EXCEEDING 4000 CHARACTERS WITH XMLAGG确实解决了这个问题。

我已经详细阐述了这个主题,如果有很长的字符串消息,这应该可以解决问题:

SELECT
   table_row_id,
   DBMS_XMLGEN.CONVERT (
     EXTRACT(
       xmltype('<?xml version="1.0"?><document>' ||
               XMLAGG(
                 XMLTYPE('<V>' || DBMS_XMLGEN.CONVERT(data_value)|| '</V>')
                 order by myOrder).getclobval() || '</document>'),
               '/document/V/text()').getclobval(),1) AS data_value
FROM (
   SELECT 1 myOrder, 1 table_row_id,'abcdefg>' data_value FROM dual
   UNION ALL
   SELECT 2, 1 table_row_id,'hijklmn' data_value FROM dual
   UNION ALL
   SELECT 3, 1 table_row_id,'opqrst' data_value FROM dual
   UNION ALL
   SELECT 4, 1 table_row_id,'uvwxyz' data_value FROM dual)
GROUP BY
   table_row_id

答案 1 :(得分:1)

SELECT Column1 , LISTAGG(Column2, ' ') 
WITHIN GROUP (ORDER BY Column2) AS employees
FROM   Table1
GROUP BY Column1 ;

<强> Please have a look at this Article

答案 2 :(得分:0)

检查LISTAGG WITH CLOB? STRING AGGREGATION EXCEEDING 4000 CHARACTERS WITH XMLAGG.这有点奇怪,但似乎有效。

再见 霍尔格

答案 3 :(得分:0)

您可以使用xmlagg来连接clob列值,以下是其对上表的查询:-

SELECT DEMO.COLUMN1, RTRIM(XMLAGG(XMLELEMENT(E, DEMO.COLUMN2, '').EXTRACT('//TEXT()') ORDER BY DEMO.COLUMN1).GETCLOBVAL(),',') AS COLUMN2
FROM DEMO_TBL DEMO
GROUP BY COLUMN1;