mardi 27 janvier 2015

expanding sql query for selecting top two rows based on rating criteria?

I am doing a few exercises to get my sql basics up. I am stuck here and unable to make any progress further. I would really appreciate if I could get tips on how to break down complex query such as the following:


There are three tables:


Movie ( mID, title, year, director ) -- There is a movie with ID number mID, a title, a release year, and a director.


Reviewer ( rID, name ) -- The reviewer with ID number rID has a certain name.


Rating ( rID, mID, stars, ratingDate ) -- The reviewer rID gave the movie mID a number of stars rating (1-5) on a certain ratingDate.


The problem is :


For all cases where the same reviewer rated the same movie twice and gave it a higher rating the second time, return the reviewer's name and the title of the movie.


Here is my attempt:



select distinct temp1.ID FROM (select * FROM
(select rID, name,title twos FROM
(select r.rID , rev.name, m.title, count(*) as twos from reviewer rev
JOIN rating r on r.rID=rev.rID
JOIN movie m on m.mID=r.mID
GROUP BY rev.rID) counts where counts.twos=2) result, rating r
where result.rID=r.rID ORDER BY ratingDate DESC) TEMP temp1
INNER JOIN TEMP temp2
ON temp1.rId = temp2.rId AND temp1.ratingDate > temp2.ratingDate
WHERE temp1.stars > temp2.stars;


I build this query iteratively. but It did not give right solution. so I would like to know how to approach this kind of problem. This is NOT homework.I am doing online tutorial from here.


Thank you


Aucun commentaire:

Enregistrer un commentaire