col1 col2
-----------------
0001 Tom
0001 Jack
0001 Mike
0002 Kate
0002 Rose
0002 Fene
如何用一条SQL语句得到如下结果:
col1 col2
-----------------
0001 Tom,Jack,Mike
0002 Kate,Rose,Fene
创建自定义函数:
create or replace function f_ename(jobno in varchar2) return varchar2
is
str_return varchar2(200);
cursor c_ename(v_jobno varchar2) is select ename from emp where job=v_jobno;
begin
for str_temp in c_ename(jobno) loop
str_return:=str_return||str_temp.ename;
end loop;
return str_return;
end;
/
调用:
select job,f_ename(job) from (select distinct job from emp);