Oracle - 嵌套表获取结果

时间:2018-01-28 18:15:12

标签: sql oracle nested

我遇到嵌套表的问题。我不知道我是否可以按照我想要的方式获取结果。 例如,我有:

create type Name as Object(
firstname varchar2(20),
lastname varchar2(20))final;

create type Author as Object(
authorName Name);

create type Author_list as table of Author;

create table books(bookID int primary key, author Author_list) nested table author store as Author_nested;

当我用以下内容获取结果时:

select b.bookID, a.authorname.firstname||' '||a.authorname.lastname
from books b, table(b.author) a;

我为每个作者找到一个特定的行。我希望对于特定的bookID,作者要在该行中显示并用逗号分隔。这可能吗?

ex:bookID,authorname         1,ab,cd,de

1 个答案:

答案 0 :(得分:0)

是的,有可能(一种方法是使用LISTAGG):

select b.bookID,
   LISTAGG(a.authorname.firstname||' '||a.authorname.lastname, ',') 
   WITHIN GROUP(ORDER BY b.BookId) AS authorname
from books b, table(b.author) a
GROUP BY b.bookID