I'm trying to get a product's name and its number of sales from two separate tables.
My tables look something like this:
BOOK
Book_ID | Book_Title | Book_Author
SOLD
Transaction_ID | Book_ID | Customer_ID
I can get most of the results I want from the following query
SELECT b.Book_Title, COUNT(s.Book_ID) FROM Book b, Sold s
WHERE b.Book_ID = s.Book_ID
GROUP BY b.Book_Title;
However, this only displays products with at least one sale. I would like to display all products, simply showing a zero if no sales have occurred. I've been messing around with something like this:
SELECT b.Book_Title,
COUNT(CASE WHEN s.Book_ID IS NULL THEN 0 ELSE s.Book_ID END)
FROM Book b, Sold s WHERE b.Book_ID = s.Book_ID GROUP BY Book_Title;
But the WHERE
clause is limiting the results to the ones with 1 or more sales.
Can anyone suggest a way around this? I am using Oracle 10g.
Thanks