sql – 如何获得此特定“查询”的结果

注意:我不小心在这里提出了另一个问题的句子(对我来说是非常道歉),我已经在3月14日星期三下午23:21更新了这篇文章,并提出了正确的问题.

我花了几个小时试图在没有任何人帮助的情况下弄清楚这个问题,但是我意识到我浪费了太多的生产时间,应该早点问别人.我有一个不错的裂缝,并且已经如此接近,但无法得到我需要的最终解决方案.我应该得到的是:

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.

这是我设法到达的查询:

SELECT reviewer.name, movie.title, rating.stars  
FROM (reviewer JOIN rating ON reviewer.rid = rating.rid)  
JOIN movie ON movie.mid = rating.mid  
GROUP BY reviewer.name 
HAVING COUNT(*) >= 2  
ORDER BY reviewer.name DESC

(我觉得上面的查询中缺少WHERE子句,但不知道放在哪里)

(据我所知,SQLite目前不支持RIGHT和FULL OUTER JOIN)

以下是表格和数据(图片中)……

……还有DB代码……

/* Delete the tables if they already exist */
drop table if exists Movie;
drop table if exists Reviewer;
drop table if exists Rating;

/* Create the schema for our tables */
create table Movie(mID int, title text, year int, director text);
create table Reviewer(rID int, name text);
create table Rating(rID int, mID int, stars int, ratingDate date);

/* Populate the tables with our data */
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming');
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas');
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise');
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg');
insert into Movie values(105, 'Titanic', 1997, 'James Cameron');
insert into Movie values(106, 'Snow White', 1937, null);
insert into Movie values(107, 'Avatar', 2009, 'James Cameron');
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg');

insert into Reviewer values(201, 'Sarah Martinez');
insert into Reviewer values(202, 'Daniel Lewis');
insert into Reviewer values(203, 'Brittany Harris');
insert into Reviewer values(204, 'Mike Anderson');
insert into Reviewer values(205, 'Chris Jackson');
insert into Reviewer values(206, 'Elizabeth Thomas');
insert into Reviewer values(207, 'James Cameron');
insert into Reviewer values(208, 'Ashley White');

insert into Rating values(201, 101, 2, '2011-01-22');
insert into Rating values(201, 101, 4, '2011-01-27');
insert into Rating values(202, 106, 4, null);
insert into Rating values(203, 103, 2, '2011-01-20');
insert into Rating values(203, 108, 4, '2011-01-12');
insert into Rating values(203, 108, 2, '2011-01-30');
insert into Rating values(204, 101, 3, '2011-01-09');
insert into Rating values(205, 103, 3, '2011-01-27');
insert into Rating values(205, 104, 2, '2011-01-22');
insert into Rating values(205, 108, 4, null);
insert into Rating values(206, 107, 3, '2011-01-15');
insert into Rating values(206, 106, 5, '2011-01-19');
insert into Rating values(207, 107, 5, '2011-01-20');
insert into Rating values(208, 104, 3, '2011-01-02');

我有另一个相对类似的问题,但如果我在这个问题上得到一些帮助,我应该可以将这个模式和技术应用到下一个.

提前致谢! 🙂

最佳答案
我添加了一个带有派生表的内连接,它返回每部电影的最大星数.由于电影和评级之间的内部联接,仅检索具有评级的电影.将其加入主查询以获得每部电影的最大明星数.

注意:您声明您希望通过电影标题订购,但您的查询订单由评论者订购.

SELECT reviewer.name, movie.title, rating.stars, maxStarsPerMovie.MaxStars
FROM (reviewer JOIN rating ON reviewer.rid = rating.rid)  
JOIN movie ON movie.mid = rating.mid  
join
(
   select movie.mid, max(rating.stars) MaxStars
   from movie
   inner join rating
      on movie.mid = rating.mid
   group by movie.mid
) maxStarsPerMovie
on movie.mid = maxStarsPerMovie.mid
ORDER BY reviewer.name DESC

编辑:要求改变了.此查询将返回评论者列表,这些评论者在以后的日期更改了他们的观点以支持电影.它是通过第二次加入评级,在星星和日期加入两个过滤器来实现的.

SELECT reviewer.name, movie.title, rating.ratingDate, rating.stars,
       newRating.ratingDate newRatingDate, newRating.Stars newRatingStars
FROM (reviewer JOIN rating ON reviewer.rid = rating.rid)  
JOIN movie ON movie.mid = rating.mid 
inner join rating newRating
     on newRating.mid = movie.mid
        and newRating.rid = reviewer.rid
        and newRating.ratingdate > rating.ratingdate
        and newRating.stars > rating.stars
ORDER BY reviewer.name, movie.title

转载注明原文:sql – 如何获得此特定“查询”的结果 - 代码日志