简体   繁体   中英

Oracle - Nested Table fetching results

I have a problem with nested tables. I don't know if I can fetch the result in the way I want them. For example I have:

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;

When I fetch the result with:

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

I am getting for each author a specific row. I want that for a specific bookID the authors to display in that row and separated with commas. Is that possible?

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

Yes, it is possible (one way is to use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM