简体   繁体   English

SQL-比较两行两列

[英]SQL - Comparing two rows and two columns

I'm studying SQL and can't seem to find an answer to this exercise. 我正在研究SQL,但似乎找不到此练习的答案。

Exercise: 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. 练习:对于同一位审阅者两次对同一部电影进行评级并第二次给予更高评级的所有情况,请返回审阅者的姓名和电影标题。

I don't know how to compare 2 rows and then get the higher rating. 我不知道如何比较2行,然后获得更高的评分。

The tables' schemas are: 表的架构为:

  • Movie ( mID, title, year, director ) English: There is a movie with ID number mID, a title, a release year, and a director. 电影(mID,标题,年份,导演)英语:有一部电影,其ID号为mID,标题,发行年份和导演。
  • Reviewer ( rID, name ) English: The reviewer with ID number rID has a certain name. 审稿人(rID,name)英文:ID号为rID的审稿人具有特定名称。
  • Rating ( rID, mID, stars, ratingDate ) English: The reviewer rID gave the movie mIDa number of stars rating (1-5) on a certain ratingDate.* 评分(rID,mID,星级,ratingDate)英语:审阅者rID在特定的ratingDate上给电影mIDa评分星级(1-5)。*

Researching here in the forum I've got as far as to this point: 到目前为止,我已经在论坛上进行了研究:

select *
from rating a
join Reviewer rv on rv.rid = a.rid
where 1 < (select COUNT(*) from rating b
            where b.rid = a.rid and b.mid = a.mid)

I'd be glad to be given also an explanation of the code. 我很高兴能得到代码的解释。 Since even the code above is making me really confused. 因为即使上面的代码也让我非常困惑。

/* 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');

something like that should work (they are other ways, too) 像这样的东西应该起作用(它们也有其他方式)

SELECT rev.name, m.title
FROM Reviewer rev
INNER JOIN Rating r1 on r1.rID = rev.rID
INNER JOIN Rating r2 on r2.rID = rev.rID and r2.mID = r1.mID
INNER JOIN Movie m on m.mID = r1.mID
WHERE r2.ratingDate > r1.ratingDate and r2.stars > r1.stars 

or you can do all in join (instead of WHERE clause) in this case 或者在这种情况下,您可以全部执行join(而不是WHERE子句)

SELECT rev.name, m.title
FROM Reviewer rev
INNER JOIN Rating r1 on r1.rID = rev.rID
INNER JOIN Rating r2 
  on r2.rID = rev.rID 
  and r2.mID = r1.mID
  and r2.ratingDate > r1.ratingDate
  and r2.stars > r1.stars
INNER JOIN Movie m on m.mID = r1.mID

SqlFiddle (with your sample datas) SqlFiddle (带有您的示例数据)

Explanation : I suppose you know the JOIN syntax, so 说明:我想您知道JOIN语法,所以

The trick is to join Rating two times. 诀窍是两次加入Rating。 Then the WHERE part checks if there's exist a line where one of the rating (from same reviewer on same movie) has a bigger ratingDate and more stars. 然后,WHERE部分检查是否存在一行,其中一个评分(来自同一电影的同一位审阅者)具有一个更大的ratingDate和更多的星星。 Which checks : "gave it a higher rating the second time". 哪个检查:“第二次给它更高的评价”。

Then we just group by reviewerName and movie title (this part is to avoid duplicates if we have 3 reviews, the second having more stars than the first, and the third more than the second) : with your sample datas, the GROUP BY is not needed, but... 然后,我们仅按reviewerName和电影标题进行分组(如果我们有3条评论,则该部分是避免重复,第二条具有比第一条更多的星星,第三条比第二条更多的星星):使用您的示例数据,需要,但是...

Start with getting all the reviewers who reviewed exactly twice: 首先让所有审阅过两次的审稿人开始:

select rid
from rating r
group by rid
having count(*) = 2

Now the question is: are they the same or is the second larger? 现在的问题是:它们是相同的还是第二个更大? To do this, join back in the ratings, but also include the two dates: 为此,请重新加入评分,但还要包括两个日期:

from (select rid, min(ratingdate) as minratingdate, max(ratingdate) as maxratingdate
      from rating r
      group by rid
      having count(*) = 2
     ) twotimes join
     rating r1
     on r1.rid = twotimes.rid and r1.ratingdate = twotimes.minratingdate join
     rating r2
     on r2.rid = twotimes.rid and r2.ratingdate = twotimes.maxratingdate

This brings in the information about the two reviews. 这带来了有关两个评论的信息。 You can finish the query from here. 您可以从这里完成查询。

You can use GROUP BY and HAVING : 您可以使用GROUP BYHAVING

SELECT m.mId, m.Title, r.Name, ra.stars
FROM Movie m 
   JOIN (SELECT mId, rId, MAX(stars) stars
        FROM Rating 
        GROUP BY mId, rId
        HAVING COUNT(*) > 1) ra ON m.mId = ra.mId
   JOIN Reviewer r ON ra.rId = r.rId
GROUP BY m.mId, m.Title, r.Name, ra.stars

This will return you any movie that has multiple reviews from the same reviewer with the highest number of stars. 这将为您返回具有来自同一评论者的最高星数的多个评论的电影。

Here is the SQL Fiddle for testing. 这是用于测试的SQL Fiddle

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM