我有一张桌子(你可以在这里看到http://sqlfiddle.com/#!9/009e8/1)和电影,每部电影都有一个类型和发布日期。
CREATE TABLE Movie (
ID int,
Title varchar(255),
Genre varchar(255),
Year int
);
INSERT into Movie VALUES(1, 'Movie 1', 'Horror', 1982);
INSERT into Movie VALUES(1, 'Movie 2', 'Drama', 1983);
INSERT into Movie VALUES(1, 'Movie 3', 'Horror', 1984);
INSERT into Movie VALUES(1, 'Movie 4', 'Comedy', 1985);
INSERT into Movie VALUES(1, 'Movie 5', 'Sci-Fi', 1986);
INSERT into Movie VALUES(1, 'Movie 6', 'Horror', 1987);
INSERT into Movie VALUES(1, 'Movie 7', 'Drama', 2010);
INSERT into Movie VALUES(1, 'Movie 8', 'Horror', 2008);
INSERT into Movie VALUES(1, 'Movie 9', 'Horror', 2006);
INSERT into Movie VALUES(1, 'Movie 10', 'Comedy', 2004);
INSERT into Movie VALUES(1, 'Movie 11', 'Horror', 2003);
INSERT into Movie VALUES(1, 'Movie 12', 'Drama', 2002);
INSERT into Movie VALUES(1, 'Movie 13', 'Comedy', 1999);
INSERT into Movie VALUES(1, 'Movie 14', 'Drama', 1967);
INSERT into Movie VALUES(1, 'Movie 15', 'Sci-Fi', 1982);
INSERT into Movie VALUES(1, 'Movie 16', 'Drama', 2009);
INSERT into Movie VALUES(1, 'Movie 17', 'Sci-Fi', 2008);
我希望能够获得具有特定类型(SELECT * FROM Movie WHERE Genre in ('Horror', 'Drama')
)的所有电影,但限制按年份排序的每种类型的结果
所以,如果我有100部戏剧电影,100部恐怖片和100部喜剧,我只想要两首最古老的戏剧和两部最古老的恐怖电影。
如何在Sql中执行此操作?如果在标准SQL中这是不可能的,我正在使用Postgres
SELECT
*
FROM (SELECT
id,
title,
genre,
year,
DENSE_RANK() OVER (PARTITION BY genre ORDER BY year) AS year_calc
FROM Movie
WHERE Genre IN ('Horror', 'Drama')) a
WHERE year_calc IN (1, 2);
Saravanan
正如@Arijit Mukherjee建议你可以使用CTE
和ROW_NUMBER()
;WITH CTE AS (
SELECT *, seq = ROW_NUMBER() OVER (PARTITION BY Genre ORDER BY Year ASC)
FROM Movie
WHERE Genre in ('Horror', 'Drama')
)
SELECT * FROM CTE WHERE seq <= 2
SELECT rank_filter.* FROM (
SELECT Movie.*,
rank() OVER (
PARTITION BY Genre
ORDER BY Year
)
FROM Movie
) rank_filter WHERE RANK IN (1,2)