你试试看: select id,replace(wm_concat(str)) from table_name group by id
这里假设第一列为id,第二列为str
用得了啊,我现在还会使用到。只是数据库版本比较高的话用listagg好点
我试了一下,oracle 11g是没有这个函数的 select id,replace(wm_concat(value)) from test group by id * 第 1 行出现错误: ORA-00938: 函数没有足够的参数
如果你想问的是如何行转列的话那么就是如下这样的语句 SQL> select * from test; ID VALUE ---------- ---------------- 1 a 1 b 1 c 1 d 1 e select id,max(case value when 'a' then value else '' end) word1, max(case value when 'b' then value else '' end) word2, max(case value when 'c' then value else '' end) word3, max(case value when 'd' then value else '' end) word4, max(case value when 'e' then value else '' end) word5 from test group by id / ID WORD1 WORD2 WORD3 WORD4 WORD5 ----- ---------- ---------- ---------- ---------- ---------- 1 a b c d e 有一个更好的例子:SQL> select * from t4; ID NAME SUBJEC GRADE ---------- ---------- ------ ----- 1 ZORRO 语文 70 2 ZORRO 数学 80 3 ZORRO 英语 75 4 SEKER 语文 65 5 SEKER 数学 75 6 SEKER 英语 60 7 BLUES 语文 60 8 BLUES 数学 90 9 PG 数学 80 10 PG 英语 90select name,sum(case subject when '语文' then grade else 0 end) 语文, sum(case subject when '数学' then grade else 0 end) 数学, sum(case subject when '英语' then grade else 0 end) 英语 from t4 group by name; NAME 语文 数学 英语 ---------- ---------- ---------- ---------- SEKER 65 75 60 BLUES 60 90 0 PG 0 80 90 ZORRO 70 80 75 我感觉这个能更好的理解行转列问题
select id,replace(wm_concat(str)) from table_name group by id
select id,replace(wm_concat(value)) from test group by id
*
第 1 行出现错误:
ORA-00938: 函数没有足够的参数
SQL> select * from test; ID VALUE
---------- ----------------
1 a
1 b
1 c
1 d
1 e
select id,max(case value when 'a' then value else '' end) word1,
max(case value when 'b' then value else '' end) word2,
max(case value when 'c' then value else '' end) word3,
max(case value when 'd' then value else '' end) word4,
max(case value when 'e' then value else '' end) word5
from test
group by id
/ ID WORD1 WORD2 WORD3 WORD4 WORD5
----- ---------- ---------- ---------- ---------- ----------
1 a b c d e
有一个更好的例子:SQL> select * from t4; ID NAME SUBJEC GRADE
---------- ---------- ------ -----
1 ZORRO 语文 70
2 ZORRO 数学 80
3 ZORRO 英语 75
4 SEKER 语文 65
5 SEKER 数学 75
6 SEKER 英语 60
7 BLUES 语文 60
8 BLUES 数学 90
9 PG 数学 80
10 PG 英语 90select name,sum(case subject when '语文' then grade else 0 end) 语文,
sum(case subject when '数学' then grade else 0 end) 数学,
sum(case subject when '英语' then grade else 0 end) 英语
from t4
group by name;
NAME 语文 数学 英语
---------- ---------- ---------- ----------
SEKER 65 75 60
BLUES 60 90 0
PG 0 80 90
ZORRO 70 80 75
我感觉这个能更好的理解行转列问题