1
votes

I'm doing a class assignment and seem to be having some trouble with one question. The question requires me two join 3 tables and count the overall number of occurrences for each row.

This is the join command I have so far (need to figure out where I can put a nested count(distinct):

SELECT litwork.btitle,
       bookcopy.copy_num,
       request.rdate
FROM   litwork,
       bookcopy,
       request
WHERE  litwork.lit_id = bookcopy.lit_id
       AND bookcopy.persidh = request.persid
ORDER  BY btitle;  

The join gives me this table as result:

BTITLE           COPY_NUM        RDATE
--------------- --------------  -------
Bankers          2                18-oct-2012
Bankers          2                30-oct-2012
Blue Ridge       1                20-oct-2012
Linux            1                18-oct-2012
Linux            1                30-oct-2012
My Life          3                31-oct-2012
O-O Design       1                30-oct-2012
O-O Design       3                25-oct-2012
O-O Design       3                18-oct-2012

So how would I make a count(distinct) for all three column within that one command? I'm using standard SQL

Edit to add question prompt (copied and paste):

Show how many requests and how many copies exist for each book that is requested. Show title, number of requests, number of copies of the book. Show in order by title. The SQL query for this question mixes a join with count. To understand such a query, be aware that the join must be processed before the count function .

Be also aware that the join may create duplicate data, so that you should use the count(distinct ..) form of count where needed. (Suggestions: if you have trouble with this question, look at the results of the join without count and group, and figure out how you would perform the required count if these results were data of an existing table. Please do not show this extra query in your report.).

Assumption: a given customer may not have more than one request of the same book.. Note that when joining the table REQUEST with the table BOOK_COPY, the number of rows where a given book appears is the product of the number of requests for that book and of the number of copies.

UPDATE: It's still not 100% correct..

Additional Tables used for the Join (First one is LITWORK table, second one is REQUEST table, and last one is BOOKCOPY table)

    LIT_ID BTITLE                YEAR
---------- --------------- ----------
      1001 The Trojans           2000
      1002 My Life               2001
      1003 Nature                1998
      1004 Blue Ridge            1996
      1005 True Java             2012
      1006 CPlus                 2004
      1007 Streaming             2000
      1008 MyApps                1998
      1009 O-O Design            2012
      1010 Camping               1978
      1011 Bankers               1970
      1012 Linux                 1962


    LIT_ID     PERSID BNAME    RDATE            RTIME
---------- ---------- -------- ----------- ----------
      1001         11 College  18-oct-2012
      1001          7 College  25-oct-2012
      1003          8 La Jolla 20-oct-2012
      1005          7 Pacific  18-oct-2012
      1008         11 Atlantic 30-oct-2012
      1008          1 College  30-oct-2012
      1012          4 La Jolla 31-oct-2012

    LIT_ID   COPY_NUM PERSID_OUT DATE_OUT    DATE_DUE       PERSIDH HDATE       BNAMEP   BNAMEC
---------- ---------- ---------- ----------- ----------- ---------- ----------- -------- --------
      1001          1          4 13-sep-2012 27-nov-2012                        La Jolla
      1002          1                                                           Pacific  Atlantic
      1002          2          1 02-sep-2012 12-oct-2012                        Pacific
      1002          3                                             4 15-nov-2012 La Jolla La Jolla
      1003          1          9 10-dec-2012 30-dec-2012                        Pacific
      1003          2          1 13-dec-2012 22-jan-2013                        La Jolla
      1003          3                                                           Atlantic Atlantic
      1004          1                                             8 19-nov-2012 Pacific  College
      1004          2          4 04-dec-2012 17-feb-2013                        Pacific
      1004          3         10 11-oct-2012 31-oct-2012                        Atlantic
      1005          1          4 27-oct-2012 10-jan-2013                        La Jolla
      1005          2          1 19-sep-2012 29-oct-2012                        Pacific
      1006          1          7 29-jan-2012 09-mar-2012                        Pacific
      1006          2          1 07-jan-2012 16-feb-2012                        College
      1006          3                                                           Pacific  College
      1007          1          1 26-oct-2012 05-dec-2012                        Pacific
      1007          2                                                           Pacific  College
      1007          3          6 15-oct-2012 04-nov-2012                        La Jolla
      1008          1          4 23-oct-2012 06-jan-2013                        College
      1008          2          3 15-oct-2012 24-nov-2012                        Pacific
      1009          1                                             1 20-nov-2012 Pacific  Pacific
      1009          2         11 28-sep-2012 12-dec-2012                        Pacific
      1009          3                                             7 22-nov-2012 La Jolla College
      1010          1          1 01-sep-2012 11-oct-2012                        Pacific
      1011          1          4 31-jan-2012 15-apr-2012                        La Jolla
      1011          2                                            11 20-nov-2012 College  La Jolla
      1012          1                                            11 19-nov-2012 Pacific  Atlantic
      1012          2          3 29-oct-2012 08-dec-2012                        Pacific

This is my current SQL command (not the one I got from Gordon Linoff's comment--that one gave me 2-3 extra rows)

select   litwork.btitle, 
         count(distinct bookcopy.copy_num), 
         count(distinct request.rdate)
from     litwork, 
         bookcopy, 
         request 
where    litwork.lit_id=request.lit_id and
         bookcopy.persidh=request.persid 
group by btitle;

And that gives me this table (which you can see I have all the correct amount of rows [when you take out the duplicates] but the count numbers are wrong)

BTITLE          COUNT(DISTINCTBOOKCOPY.COPY_NUM) COUNT(DISTINCTREQUEST.RDATE)
--------------- -------------------------------- ----------------------------
Linux                                          1                            1
MyApps                                         2                            1
Nature                                         1                            1
The Trojans                                    3                            2
True Java                                      1                            1
1
. . If you are learning SQL, why are you not learning correct join syntax? You should be using JOIN with an ON clause, not implicit joins in the WHERE clause. And, yes, you can ask your teacher this question. - Gordon Linoff
what do you want the result to look like? - Taryn
What else have you tried? What research have you done? I recommend only asking on SO if you've exhausted all other resources. - That1Guy
@Gordon Linoff: This is what all of his examples (in the book he wrote) were like... No one in the class can understand him because of his accent so we basically have to teach ourselves. I can post up the question prompt. - eehyunh
@That1Guy: I've been on this question for the past 3 hours because his book isn't very clear--they're basically notes he wrote and put together into a booklet for us. I've tried searching google but can't seem to find an example similar to this one - eehyunh

1 Answers

-2
votes

The following should work with Oracle's SQL *Plus:

select litwork.btitle, count(distinct bookcopy.copy_num), count(distinct request.rdate)
from litwork, bookcopy, request
where litwork.lit_id = request.lit_id and request.lit_id = bookcopy.lit_id(+)
group by litwork.btitle;

For more info on joins see Oracle SQL*Plus Pocket Reference, 2nd Edition.

Possible solutions to count issue:

  • Change count(distinct request.rdate) to count(distinct request.PERSID).
  • Change count(distinct request.rdate) to count(distinct request.*).
  • Change count(distinct request.rdate) to sum(case when request.rdate is null then 0 else 1 end)
  • Instead of joining the request table you could join (select lit_id, count(*) as requestCount from request group by lit_id) then select requestCount.

This is probably the best I can do since I do not have the detailed definition of the fields and how they relate. It would also help if I had the expected results and SQL *Plus to test it with.