SQL query help [message #636163] |
Sat, 18 April 2015 09:26 |
|
gorants
Messages: 85 Registered: May 2014 Location: ATL
|
Member |
|
|
Hello All,
I have table books which has columns isbn,booktitle,authors and another table reviews with columns rating and isbn
i need to write query to pull isbn,booktitle,authors from books and avg rating from reviews table.
reviews talbe will have multiple ratings on one isbn so i need take avg_rating.
How can i combine books and review table here ? to get avg_rating from reviews table.
Below is the query i have written but it is not giving expected results
SELECT DISTINCT(
(SELECT count(*) from books b where UPPER(b.booktitle) like UPPER("%pat%") OR UPPER(b.Authors) like UPPER("%pat%")) As TotalCount,
x.*,
b.isbn,
b.booktitle,
b.authors,
from books b,
(select ROUND(AVG(rating),0)avg_rate,isbn from reviews group by isbn)x )
where b.isbn = x.isbn
and UPPER(b.booktitle) like UPPER("%pat%") OR UPPER(b.Authors) like UPPER("%pat%")
|
|
|
|
Re: SQL query help [message #636168 is a reply to message #636163] |
Sat, 18 April 2015 12:20 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
It looks like what you have done is unnecessarily over-complicated. All you should need is a join and avg. The following eliminates the unnecessary parts from your code and leaves a couple of blanks for you to fill in.
SELECT b.isbn, b.booktitle, b.authors,
AVG (...)
FROM books b, reviews x
WHERE b.isbn = x.isbn
GROUP BY ...;
|
|
|