日期:2014-05-17  浏览次数:20736 次

急!求教高手oracle9i将多列合并为一列
SQL> create table temp(index_id integer,class_name varchar2(20));

表被创建

SQL> select * from temp;

  INDEX_ID CLASS_NAME
--------------------------------------- --------------------
  1 aaaaaaaa
  1 bbbbbbbb
  1 ccccccccc
  2 dddddddd
  2 rrrrrrrtyyyyy
  2 fffffffffhhhhh
  3 uuuuuuuuuu
  3 uuuuuuuuu
要求得到的结果为:按index_id分组group by index_id,得到的新表为:
  index_id 新字段
  1 aaaaaaaa,bbbbbbbb,ccccccccc
  2 dddddddd,rrrrrrrtyyyyy,fffffffffhhhhh
  3 uuuuuuuuuu,uuuuuuuuu
可否实现?

------解决方案--------------------
SQL code
select INDEX_ID,max(substr(sys_connect_by_path(CLASS_NAME,','),2))
 from temp
 start with index_id=1 and prior index_id=index_id
 group by index_id

------解决方案--------------------
select INDEX_ID , wm_concat(CLASS_NAME)
 from temp group by INDEX_ID