我输入
12 abc
12 def
34 ghi
我希望输出为
12 abc,def
34 ghi
如何使用sql.Thanks实现这一点!
答案 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;
您可以在此处找到其他替代方案:[http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php]
答案 1 :(得分:1)
如果您使用的是oracle 11g
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;