我在编写PL / SQL语句时难以显示以下输出。
这些是我的表格:
我需要显示输出如下:
1 Mary Loo:Sewing,Dancing,Cooking 2 Peter Pan:Painting, Singing ...
我该怎么做?
由于
答案 0 :(得分:2)
不需要PL / SQL,只需使用LISTAGG。
create table employee (id number, fname varchar2(20), lname varchar2(20), primary key (id));
create table skillpossessed(employee_id number, sname varchar2(20), lvl number, constraint fk_employee_id foreign key(employee_id) references employee(id));
insert into employee values (1, 'Mary', 'Loo');
insert into employee values (2, 'Peter', 'Pan');
insert into skillpossessed values (1, 'Sewing', 1);
insert into skillpossessed values (1, 'Dancing', 2);
insert into skillpossessed values (1, 'Cooking', 5);
insert into skillpossessed values (2, 'Painting', 3);
insert into skillpossessed values (2, 'Singing', 1);
select e.id, e.fname, e.lname, listagg (s.sname, ',')
within group (order by s.sname) as skills
from employee e, skillpossessed s
where e.id = s.employee_id group by e.id, e.fname, e.lname;
ID FNAME LNAME SKILLS
---------- ------ ----- ----------------------
1 Mary Loo Cooking,Dancing,Sewing
2 Peter Pan Painting,Singing