日期:2014-05-16  浏览次数:20915 次

ORACLE 新手求助 查询结果表按照分号分隔开并分组问题

请教各位大仙 

oracle查询结果表中有三列数据

列一  列二 列三 
11    22   A1 
11    22   A2
11    22   A3


请问如何将列三 中的数据 A1,A2,A3按照分号分割开
并按照列一,列二分组 显示成一行

如 11  22    A1,A2,A3 这种形式 
------最佳解决方案--------------------

with t(col1,col2,col3) as(
select 11,22,'A1' from dual
union all select 11,22,'A2' from dual
union all select 11,22,'A3' from dual
)
select col1,col2,wm_concat(col3) col3 from t group by col1,col2;

------其他解决方案--------------------

--wm_concat排序是很乱的,如果需要指定排序,如
with t(col1,col2,col3) as(
select 11,22,'A1' from dual
union all select 11,22,'A2' from dual
union all select 11,22,'A3' from dual
)
select col1,col2,max(col3) col3
from (select col1,col2,wm_concat(col3) over (partition by col1,col2 order by col3) col3 from t)
group by col1,col2;

------其他解决方案--------------------
可以通过将行合并到列的方法定义strcat函数,在网上可以查到;
然后select 列一 列二 strcat(列三) from table group by 列一 列二 就可以了,
使用union 的方式如果有很多行,处理起来很麻烦。
------其他解决方案--------------------
刚才忘了加逗号:
select 列一,列二,strcat(列三) from table group by 列一,列二 
注意在strcat中可以定义是用什么符号连接列三
引用:
可以通过将行合并到列的方法定义strcat函数,在网上可以查到;
然后select 列一 列二 strcat(列三) from table group by 列一 列二 就可以了,
使用union 的方式如果有很多行,处理起来很麻烦。

------其他解决方案--------------------
http://blog.csdn.net/lxpbs8851/article/details/7179291