SQL - 返回编号最大的标题

时间:2017-01-20 15:39:54

标签: sql postgresql greatest-n-per-group

我想从我的数据库中返回最多eps的标题。 使用以下代码,我将获得所有标题。

SELECT titel, MAX(aantalafleveringen) FROM imdb.tvserie GROUP BY titel;

希望有人能解释我的错误。

3 个答案:

答案 0 :(得分:1)

像他一样:

SELECT distinct titel, MAX(aantalafleveringen) over (partition by titel) 
FROM imdb.tvserie
ORDER BY max desc
LIMIT 1
;

答案 1 :(得分:1)

使用Order byLimit

SELECT titel,
       Max(aantalafleveringen) AS max_aantalafleveringen
FROM   imdb.tvserie
GROUP  BY titel
ORDER  BY max_aantalafleveringen DESC -- orders the result in descending order
LIMIT 1 -- filters the first record

答案 2 :(得分:1)

如果您希望每组最多最多,那么您真的想要全局最大值。

这与其他答案相同,但更为简单:

SELECT   titel, aantalafleveringen
FROM     imdb.tvserie
ORDER BY aantalafleveringen DESC
LIMIT    1