我輸入作爲SQL行列從行數據conversion.converting到列
12 abc
12 def
34 ghi
我想輸出
12 abc,def
34 ghi
如何使用sql.Thanks實現這一目標!
我輸入作爲SQL行列從行數據conversion.converting到列
12 abc
12 def
34 ghi
我想輸出
12 abc,def
34 ghi
如何使用sql.Thanks實現這一目標!
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;
我認爲,問題不在於將行轉換爲列(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]
很好,但恐怕其他答案也一樣! –