CaptainKebab
Programmer
Hi all, I'm writing the reports section for a bookstore app and I'm trying to figure out how to do a query only in sql (as opposed to loops in the code).
I want to show the top seller (book) for each book classification of book that the store sells.
This query involves two tables:
Book: ISBN, Title, Classification, etc
Order_Details: Order_No, ISBN, Copies, etc
What I need is a dataset returned like so:
Classification | ISBN | Title | Copies Sold
Fiction | 321.a | My Book | 327
Science | 156.b | Math 2 | 186
I've been fiddling around with it for a while but the closest I can get returns all books for each classification and the amount of copies sold for each book, or the amount of books sold in each classification without the top book.
It probably won't help but here are those two closest queries:
SELECT Book.Classification, Book.ISBN, COUNT(Order_Details.Copies) AS 'Total_Sales'
FROM Book, Order_Details
WHERE Book.ISBN = Order_Details.ISBN
GROUP BY Book.ISBN, Book.Classification
ORDER BY 'Total_Sales' DESC
and
SELECT DISTINCT(Book.Classification), COUNT(Order_Details.ISBN) * Order_Details.Copies AS 'Total_Sales'
FROM Book, Order_Details
WHERE Order_Details.ISBN = Book.ISBN
GROUP BY Book.Classification, Order_Details.Copies
Any help would be greatly appreciated, thanks!
I want to show the top seller (book) for each book classification of book that the store sells.
This query involves two tables:
Book: ISBN, Title, Classification, etc
Order_Details: Order_No, ISBN, Copies, etc
What I need is a dataset returned like so:
Classification | ISBN | Title | Copies Sold
Fiction | 321.a | My Book | 327
Science | 156.b | Math 2 | 186
I've been fiddling around with it for a while but the closest I can get returns all books for each classification and the amount of copies sold for each book, or the amount of books sold in each classification without the top book.
It probably won't help but here are those two closest queries:
SELECT Book.Classification, Book.ISBN, COUNT(Order_Details.Copies) AS 'Total_Sales'
FROM Book, Order_Details
WHERE Book.ISBN = Order_Details.ISBN
GROUP BY Book.ISBN, Book.Classification
ORDER BY 'Total_Sales' DESC
and
SELECT DISTINCT(Book.Classification), COUNT(Order_Details.ISBN) * Order_Details.Copies AS 'Total_Sales'
FROM Book, Order_Details
WHERE Order_Details.ISBN = Book.ISBN
GROUP BY Book.Classification, Order_Details.Copies
Any help would be greatly appreciated, thanks!