I have 3 tables:
biblio(biblionumber, title);items(itemnumber, biblionumber);old_issues(issues_id, itemnumber, issuedate)
biblio has a one to many relationship to items.
items has a one to many relationship to old_issues.
I want to know how many items are in the biblio table per unique biblio title, and how many times the item has been issued within a date range. Then in the last column I want the ratio of items versus the number of times it's been issued:
biblio title | Copies of title (Count of items in biblio) | Count of issues | count of items / count of issues
The problem I'm having is that if I join all the tables, I won't get an accurate count of how many items are in the biblio table as they will only be counted if they have been issued.
For example, if I have one title "Terminator 2" (one unique entry in the biblio table) and it has 5 copies (5 unique entries in the items table), but none of these copies have been issued before, then I will get a count of 0 items.
I should end up with a result like:
"Terminator 2" has 5 copies in the library, and has been issued 245 times in the year 2017. That is a ratio of 0.02 meaning we have to order more copies to reach demand!
I'm thinking I have to do a subquery/nested query of some kind that is independent of the results in the other table, but somehow join the table together to get one view. I'm too amateur to know how to do that, so any help would be appreciated. Sorry if my explanation is hard to understand.
Here's the query I'm trying to work with:
SELECT biblio.title, COUNT(items.itemnumber), COUNT(old_issues.itemnumber)
FROM old_issues
JOIN items ON old_issues.itemnumber = items.itemnumber
JOIN biblio ON items.biblionumber = biblio.biblionumber
WHERE old_issues.issuedate BETWEEN '2018-02-01' AND '2018-02-03'
GROUP BY biblio.biblionumber
count(distinct FIELD)