如何在 oracle 中连接由 space 分隔的行?
How to concat rows separated by a space in oracle?
我正在尝试将 table 中的 concat/merge 行合并为一行。我尝试使用 listagg,但由于 varchar 的限制,这不起作用。
create table tmp(word VARCHAR2(4000),
lvl NUMBER);
insert into tmp2 values('python',1);
insert into tmp2 values('java',2);
select listagg(word,' ') within group(order by lvl) as listagg_output from tmp;
输出应该类似于 python java.
你会用这么长的字符串做什么?
无论如何,看看这个例子;如果 listagg
行不通,xmlagg
行。
SQL> create table test (id, col) as
2 select rownum, a.column_name
3 from user_tab_columns a cross join user_tab_columns b
4 cross join user_tab_columns c;
Table created.
SQL> select count(*) from test;
COUNT(*)
----------
9261
SQL> select listagg(col, ' ') within group (order by null) result from test;
select listagg(col, ' ') within group (order by null) result from test
*
ERROR at line 1:
ORA-01489: result of string concatenation is too long
SQL> select length(xmlagg(xmlelement(e, col, ' ').extract('//text()') order by null).GetClobVal()) length_result
2 from test;
LENGTH_RESULT
-------------
51156
SQL>
你的问题很简单:
您创建了 table tmp
您将测试数据插入 table tmp2
您对 tmp
的查询未返回任何结果,因为 tmp
中没有数据
解决办法是将你的测试数据插入tmp
:
create table tmp(word VARCHAR2(4000),
lvl NUMBER);
insert into tmp values('python',1);
insert into tmp values('java',2);
select listagg(word,' ') within group(order by lvl) as listagg_output from tmp;
现在这个returnspython java
我正在尝试将 table 中的 concat/merge 行合并为一行。我尝试使用 listagg,但由于 varchar 的限制,这不起作用。
create table tmp(word VARCHAR2(4000),
lvl NUMBER);
insert into tmp2 values('python',1);
insert into tmp2 values('java',2);
select listagg(word,' ') within group(order by lvl) as listagg_output from tmp;
输出应该类似于 python java.
你会用这么长的字符串做什么?
无论如何,看看这个例子;如果 listagg
行不通,xmlagg
行。
SQL> create table test (id, col) as
2 select rownum, a.column_name
3 from user_tab_columns a cross join user_tab_columns b
4 cross join user_tab_columns c;
Table created.
SQL> select count(*) from test;
COUNT(*)
----------
9261
SQL> select listagg(col, ' ') within group (order by null) result from test;
select listagg(col, ' ') within group (order by null) result from test
*
ERROR at line 1:
ORA-01489: result of string concatenation is too long
SQL> select length(xmlagg(xmlelement(e, col, ' ').extract('//text()') order by null).GetClobVal()) length_result
2 from test;
LENGTH_RESULT
-------------
51156
SQL>
你的问题很简单:
您创建了 table
tmp
您将测试数据插入 table
tmp2
您对
tmp
的查询未返回任何结果,因为tmp
中没有数据
解决办法是将你的测试数据插入tmp
:
create table tmp(word VARCHAR2(4000),
lvl NUMBER);
insert into tmp values('python',1);
insert into tmp values('java',2);
select listagg(word,' ') within group(order by lvl) as listagg_output from tmp;
现在这个returnspython java