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
640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12332752
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
ID: 12334246
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
ID: 12334253
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12334266
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
ID: 12334269
why do you want all the details on one line?
0
 
LVL 6

Accepted Solution

by:
izblank earned 500 total points
ID: 12340623
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
ID: 12346293
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

739 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