2014-07-17 27 views

回答

1

create table test(id int,val char(3)); 

insert into test values(12,'abc'); 
insert into test values(12,'def'); 
insert into test values(34,'ghi'); 

查詢

SELECT id, LISTAGG(val, ',') WITHIN GROUP (ORDER BY val) AS values 
FROM test 
GROUP BY id; 

SQL Fiddle Demo

+0

很好,但恐怕其他答案也一樣! –

2

我認爲,問題不在於將行轉換爲列(PIVOT),而是關於聚合字符串。 如果你在11.2您可以使用LISTAGG:

with q as (select '12' id, 'abc' col from dual 
union all 
select '12' id, 'def' col from dual 
union all 
select '34' id, 'ghi' col from dual ) 
select id, listagg(col,',') within group (order by col) col_agg 
from q; 

你可以在這裏找到另一個替代方案:如果您在使用Oracle 11g [http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php]