简体   繁体   English

Oracle-嵌套表获取结果

[英]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. 我希望针对特定bookID的作者显示在该行中并以逗号分隔。 Is that possible? 那可能吗?

ex: bookID, authorname 1 , ab, cd, de 例如:bookID,作者名1,ab,cd,de

Yes, it is possible (one way is to use LISTAGG ): 是的,这是可能的(一种方法是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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