Home » SQL & PL/SQL » SQL & PL/SQL » SQL query help (oracle 11g)
SQL query help [message #636163] Sat, 18 April 2015 09:26 Go to next message
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 #636165 is a reply to message #636163] Sat, 18 April 2015 09:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Below is the query i have written but it is not giving expected results

We don't have your tables.
We don't have your data.
We don't know what the "expected results" should be.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

>(SELECT count(*)

I don't see in problem statement any mention of COUNT so why is it included query?

Re: SQL query help [message #636168 is a reply to message #636163] Sat, 18 April 2015 12:20 Go to previous message
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 ...;

Previous Topic: MAT view - Start with & Next
Next Topic: multilagual charactor
Goto Forum:
  


Current Time: Tue Apr 23 03:31:37 CDT 2024