If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Finding a most occuring item (HELP)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-09, 23:04
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
Finding a most occuring item (HELP)

Hello,

I am trying to work on my SQL database that I've set up that is supposed to be exampling a library system.

Document(document#, document_title, document_type, document_descriptors)

Borrowing(reader#, document#, copy#, date_borrowed, date_returned)

I'm supposed to put through a query that returns the document(s) that is(are) the most borrowed. I would have to compare the copy#'s to see what document_title has the most copy# that have a copy_status of 'borrowed'. How exactly would I do this in SQL?

So far I have the following:

Code:
SELECT D.document_title, COUNT(*) as "Times borrowed"
FROM Document D, Borrowing B
WHERE D.`document#` = B.`document#`
GROUP BY D.document_title
ORDER BY COUNT(*) DESC LIMIT 1
This gives me the highest borrowed document, but if there were two I wouldn't see that because of the limit. Is there anyway to alter this to show which document(s) is(are) the most borrowed?

Last edited by dritzthevampyr; 10-08-09 at 07:39.
Reply With Quote
  #2 (permalink)  
Old 10-07-09, 23:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by dritzthevampyr
I'm supposed to put through a query that returns the document that is the most borrowed.
having trouble with your homework assignment?

do not just ask for the answer, we will not hand it to you

~you~ must do the work to come up with a solution

we will only guide you and help you to understand

what have you tried so far?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-07-09, 23:27
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
void void void

Last edited by dritzthevampyr; 10-08-09 at 07:40.
Reply With Quote
  #4 (permalink)  
Old 10-07-09, 23:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
that's a good observation -- LIMIT will not show ties properly, so you shouldn't be using LIMIT

you've correctly figured out that GROUP BY is needed, but i don't understand why you have three tables in your FROM clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-07-09, 23:43
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
void void void

Last edited by dritzthevampyr; 10-08-09 at 07:40.
Reply With Quote
  #6 (permalink)  
Old 10-08-09, 00:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you're not on the right track yet

the status column shows you only the current status of a copy

the question asks which document is most borrowed

you cannot tell how often a copy has been borrowed by looking at its current status
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-08-09, 01:25
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
void void void

Last edited by dritzthevampyr; 10-08-09 at 18:59.
Reply With Quote
  #8 (permalink)  
Old 10-08-09, 07:02
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
void void void

Last edited by dritzthevampyr; 10-08-09 at 19:00.
Reply With Quote
  #9 (permalink)  
Old 10-08-09, 07:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
we're all still asleep, keep your pants on
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-08-09, 07:58
dritzthevampyr dritzthevampyr is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
void void void

This thread is done. Not much assistance was provided. Solution found in a different forum.

Last edited by dritzthevampyr; 10-08-09 at 19:02.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On