我有一张表格如下:
Create Table Movies (
ID int,
Actor varchar (50),
MovieName varchar (50),
ReleaseDate Date
)
让我们插入一些样本数据
Insert into Movies (ID,Actor,MovieName, ReleaseDate)
Select 1,'Sean Connery', 'Gone gone wild', '2/1/2014'
Union
Select 2,'Roberta Jones', 'My first love', '2/1/2014'
Union
Select 3,'Michael Young', 'Booba spirit', '2/1/2014'
Union
Select 4,'Sean Conenry', 'Gone gone wild 2', '10/1/2014'
Union
Select 5,'Roberta Jones', 'My Second Love', '2/3/2015'
Union
Select 6,'Michael Young', 'Boboa spirit 2', '2/2/2016'
Union
Select 7,'Sean Connery', 'Gone gone wild 3', '2/1/2015'
Union
Select 8,'Sean Connery', 'Gone gone wild 4', '2/1/2016
我想知道演员,每个演员每年的发行日期和电影数量,但只返回电影数量最多的一年。
这是一个在SQL Server上运行良好的查询
Select * from Movies
select Actor, Release, NbrMovies
From (Select Actor, Release, NbrMovies, rank() over (partition by Actor Order By NbrMovies desc) as rn
From (Select Actor, year(ReleaseDate) as Release, count(*) as NbrMovies
From Movies
Group by Actor, year(ReleaseDate)
) x
) y
where rn = 1
我想知道是否有一种方法可以在没有任何窗口函数(如rank()或row_number()的情况下编写查询,因为我也将它用于Oracle。
由于
答案 0 :(得分:0)
您的查询适用于SQL Server和Oracle。但是,您可以删除一层子查询:
select Actor, ReleaseYear, NbrMovies
from (Select Actor, year(ReleaseDate) as ReleaseYear,
count(*) as NbrMovies,
rank() over (partition by Actor Order By count(*) desc) as rn
From Movies m
Group by Actor, year(ReleaseDate)
) m
where rn = 1;