Sql 查找同一工作室发布的所有电影

Sql 查找同一工作室发布的所有电影,sql,Sql,我的问题如下表所示: Movie Studio Year Rating The Shining Warner 1999 8.4 The Pianist Warner 2002 8.6 The LOTR Dreamworks 2004 8.7 License to Kill Dreamworks 2001 8.3 Complicated HP 2005

我的问题如下表所示:

Movie             Studio    Year    Rating
The Shining       Warner    1999     8.4
The Pianist       Warner    2002     8.6
The LOTR        Dreamworks  2004     8.7
License to Kill Dreamworks  2001     8.3
Complicated         HP      2005     7.7
Django Unchained    HP      2003     8.8
Diamonds        John Lewis  1997     6.7
你如何找到比之前发行的所有电影都有更高评级的电影 同一家电影制片厂拍的电影

因此,我想,输出应该是:《钢琴家》,因为它比另一部电影《闪灵》发行得晚,由同一家制片厂发行,而且收视率更高。 LOTR的原因与上述相同

Django没有受到限制,因为尽管Complex后来发布,但它的评级比Complcated低。这也是该制片厂最老的电影,所以我不知道该怎么处理


最后,应该返回钻石,因为它是该工作室的唯一电影,没有什么可比性。

此解决方案将与支持窗口功能的RDBMS一起使用

WITH cte AS
(
   SELECT *, rn = RANK() OVER (PARTITION BY Studio ORDER BY Rating DESC)
   FROM #mytable
)
SELECT *
FROM cte
WHERE rn = 1
如果同一工作室没有更高评级的旧电影,则将返回一部电影。这就是你想要的吗?

对于你的原始陈述:

关于应提出django的第二项声明:


如果你想每个工作室只归还一部电影,你必须决定什么是最重要的——最高评级还是最新的电影。你确定t2.yeart1s年year@Thomas,我的选择正如我所描述的那样有效,也就是说,如果同一家制片厂没有更高评级的老电影,就返回一部电影。啊,我从另一个角度想了想。tnx!对不起,没有早点回复。我相信这回答了我的问题,我接受了。再一次,抱歉花了这么长时间。
select * from movietable t1
where not exists (select 1 from movietable t2
                  where t2.studio = t1.studio
                    and t2.rating > t1.rating
                    and t2.year < t1.year)
select * from movietable where not exists (select 1 from movietable t2 where
movietable.studio = t2.studio and movietable.year < t2.year)
select * from movietable where not exists (select 1 from movietable t2 where
movietable.studio = t2.studio and movietable.year < t2.year
and movietable.rating <= t2.rating
)