Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Obtain Sum using 2 Tables

Author  Topic 

gretty
Starting Member

6 Posts

Posted - 2009-10-16 : 00:09:37
Hello, I hope this makes sense :P

My objective is to:
quote:
List the book code, book title and the total number of copies on hand of each book. Order the list by book code.


I have 2 tables;
- book (where columns book_code & title are located)
- inventory (where columns on_hand & book_code(exactly the same as book_code from the previous table)

My problem is that I always get an error when I try to get the sum of total number of copies on hand of each book

Heres what I have which wont work: Any advice?

select book.book_code, title, sum(on_hand)
from book, inventory
where book.book_code = inventory.book_code
order by book_code asc;


Also this wont work also:
select book.book_code, title, sum(inventory.book_code*inventory.on_hand)
from book, inventory
where book.book_code = inventory.book_code
order by book_code asc;

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2009-10-16 : 01:33:40
SELECT book.book_code, title, sum(on_hand)
FROM book INNER JOIN inventory
ON book.book_code = inventory.book_code
GROUP BY book_code,title;
Go to Top of Page
   

- Advertisement -