I have two tables:
BOOK
BOOK_CODE TITLE PUBLISHER_CODE TYPE PRICE PAPERBACK
--------- ----- -------------- ---- ----- ---------
0189 Magic FA HOR 7.99 Yes
0378 Venice FA ART 24.50 No
0808 The Edge JP MYS 6.99 Yes
6128 Jazz PL FIC 12.99 Yes
0200 Start HQ FIC 8.99 Yes
INVENTORY
BOOK_CODE BRANCH_NUM ON_HAND
--------- ---------- -------
0189 1 2
0189 2 2
0200 1 11
6128 3 7
I was wanting to display the title, book_code and the total number of copies available for each book.
I can do them separately… I can get the number of each book and the book_code with:
SELECT inventory.book_code,
SUM(on_hand)
FROM inventory
GROUP BY inventory.book_code;
But how do I get the title to display next to these results? I tried using join and tried selecting it in the initial select statement but I get a “not a GROUP BY expression” error.
Use a derived table to get the sum – this example uses an OUTER join in case you want to see books without any
INVENTORYrecords:Otherwise, you can use an INNER join.