Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can this be done?

How can this be done?

How can this be done?

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


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!

RE: How can this be done?

A simple way to solve this is to create a view which does the calculations, and then use it to find me most sold books.

create view books_sold as
select b.*, sum(o.Copies) as Total_Sales
from book as b left join Order_Details as o on b.ISBN = o.ISBN
group by b.ISBN,b.Title, b.Classification;

Then do something like
select * from books_sold as bs
where Total_Sales = (select max(Total_Sales) from books_sold
                     where Classification = bo.Classification);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close