Solved

ORACLE: Able to show multiple 'matches' of one attribute in one row without knowing how many matches there are

Posted on 2004-10-17
7
633 Views
Last Modified: 2008-01-09
Hi
I've got six relations as shown below (i've only included relevant attributes). Each book could have multiple authors and multiple subjects. I need to be able to display all of a books details in one row, searched by callNo and including the display of title, author name(s), subject name(s) and publisherName. I have worked out how to do it if I know exactly how many authors and subjects it has (see below, this one has two authors and two subjects), but I can't work out what to do to get it to work without knowing the number of authors or subjects.
                        Thankyou Roxanne

THESE ARE MY TABLES
book( call_No, title, pub_id)
publisher(pub_Name)
subject(sub_id, sub_name)
bookSubject(call_no, sub_id)
authorOfBook(call_no, auth_id)
author(auth_id, auth_name)


THIS IS MY SQL STATEMENT
SELECT b.call_No, b.title, p.pub_Name,
a1.auth_Name || ', '||a2.auth_Name, s1.sub_Name || ', '||
s2.sub_Name
FROM author a1, author a2
book b, publisher p, subject s1, subject s2
authorOfBook ab, bookSubject bs
WHERE
b.call_No = '300A' AND
bs.call_No = b.call_No AND
ab.call_No = b.call_No AND
s1.sub_Id in ( select bs.sub_Id from bookSubject bs WHERE bs.call_No = '300A') AND
s2.sub_Id in ( select bs.sub_Id from bookSubject bs WHERE bs.call_No = '300A') AND
s1.sub_Id < s2.sub_Id AND
b.pub_Id = p.pub_Id AND
a1.auth_Id in ( select ab.auth_Id from authorOfBook ab WHERE ab.call_No = '300A') AND
a2.auth_Id in ( select ab.auth_Id from authorOfBook ab WHERE ab.call_No = '300A') AND
a1.auth_Id < a2.auth_Id
GROUP BY b.call_No, b.title, p.pub_Name,
a1.auth_Name || ', '||a2.auth_Name, s1.sub_Name || ', '||
s2.sub_Name

0
Comment
Question by:RoxanneMcCafferty
  • 4
  • 2
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
you should have additional tables BookAuthor (Call_No, Auth_iD) and BookSubject (Call_no, Sub_ID)  to resolve the potential for Many-to-many relationships between books and authors (one book can have 1 or more authors, and any one author may have written 1 or more books), and also between Books and Subjects (a boiok can have 1 or more subjects, and any given subject may be addressed by 1 or more books)  Then you can have as many authors as needed for any one book, and as many subjects as needed for any one book.

AW
0
 

Author Comment

by:RoxanneMcCafferty
Comment Utility
That is why there is an author table and a authorOfBook table and a sujbect table and a bookSubject table. I can have as many as I want in the tables, the issue is the getting a query to show all of a single books details so it shows like below in one row:
   
      callNo     title       pub_name    author1, author2 etc...     subject1, subject2, subject3 etc...
       300A    Hello       Johnstone      Rose Camwell, Sarah Camwell       History, Politics, Religion
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
the two tables that I suggested, you already have, so what you need to do is:

Select Book.Title, author.name, subject.sub_name from book join AuthorofBookon book.call_no  = authorOfBook.call_no join author on authorofbook.auth_id = author.auth_id join booksubject on book.call_no = booksubject.call_no join subject on booksubject.sub_id = subject.sub_id

AW
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
you probably can't get all of the details on one line, without resorting to s Stored Procedure.

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
why do you want all the details on one line?
0
 
LVL 6

Accepted Solution

by:
izblank earned 500 total points
Comment Utility
If you're on 9i, you can define an aggregate function to concatenate strings into a comma-delimited list.  Then, you simply run this:

SELECT b.call_No, b.title, p.pub_Name, strAggr(a.auth_name), strAgg(sub_name)
FROM author a,
book b, publisher p, subject s,
authorOfBook ab, bookSubject bs
WHERE
b.call_No = '300A' AND
bs.call_No = b.call_No AND
ab.call_No = b.call_No AND
and s.sub_Id = bs.sub_id
and bs.call_no=b.call_no
b.pub_Id = p.pub_Id AND
a.auth_Id = ab.auth_id AND  
ab.call_No = b.call_no AND
GROUP BY b.call_No, b.title, p.pub_Name


In 8i, you define 2 functions:

CREATE FUNCTION auth_list
(call_no_in in number) return VARCHAR2
AS
   retVal varchar2(2000);
BEGIN
     FOR rec in (select auth_name
             FROM author a, authorOfBook ab
             WHERE ab.call_no=call_no_in
             AND a.auth_id=ab.auth_id)
    LOOP
          retVal := retVal || rec.auth_name||',';
    END LOOP;
   RETURN RTRIM(retVal,',');
END;

CREATE FUNCTION sub_list
(call_no_in in number) return VARCHAR2
AS
   retVal varchar2(2000);
BEGIN
     FOR rec in (select sub_name
             FROM subject s, bookSubject bs
             WHERE bs.call_no=call_no_in
             AND s.sub_id=bs.sub_id)
    LOOP
          retVal := retVal || rec.sub_name||',';
    END LOOP;
   RETURN RTRIM(retVal,',');
END;


Then you run this:

SELECT b.call_No, b.title, p.pub_Name, auth_list(b.call_no), sub_list(b.call_no)
FROM book b, publisher p
WHERE
b.call_No = '300A' AND
b.pub_Id = p.pub_Id AND

0
 

Author Comment

by:RoxanneMcCafferty
Comment Utility
Thanks for your responses arthur and izblank

A friend of mine solved my problem in a very similar way to izblank's solution and it works well, the code I used for anyone who searches this question was as follows:

CREATE OR REPLACE TYPE string_list_t AS TABLE OF VARCHAR2(50);
/
CREATE OR REPLACE
Function       CONCAT_LIST
  ( lst IN string_list_t, separator varchar2)
  RETURN  VARCHAR2 IS
   ret                 varchar2(400) := '';
BEGIN
  IF lst.COUNT != 0 THEN
    FOR j IN 1..lst.LAST  LOOP
        ret := ret || lst(j) || separator ;
    END LOOP;
  END IF;
  RETURN ret;
END;
/

SELECT b.call_no, b.title, p.pub_name as publisher, CONCAT_LIST(authors, ', ') as authors, CONCAT_LIST(subjects, ', ') as subjects
FROM publisher p, book b, (SELECT b.call_no,
                           CAST(MULTISET(SELECT auth_name
                             FROM   authorOfBook, author
                             WHERE  authorOfBook.call_no = b.call_no
                             AND    authorOfBook.auth_id = author.auth_id) AS string_list_t) authors,
                            CAST(MULTISET(SELECT sub_name
                             FROM   bookSubject, subject
                             WHERE  bookSubject.call_no = b.call_no
                             AND    bookSubject.sub_id = subject.sub_id) AS string_list_t) subjects                    
                          FROM   book b
                          WHERE b.call_no = '300A'
                          GROUP BY b.call_no )
WHERE b.call_no = '300A'  
AND   p.pub_id = b.pub_id
;
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now