2017-01-20 28 views
0

我想從我的數據庫中返回最多eps的標題。 使用下面的代碼,我確實獲得了所有的標題。SQL - 返回最高編號的標題

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

希望有人能解釋我做錯了什麼。

+0

很多很多的答案是:https://stackoverflow.com/questions/tagged/greatest-n-per-group+ postgresql –

回答

1

如果你想最大按組最大的,你真的不想在全球最大。

這相當於與其他的答案,但更簡單:

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

水木清華就像他:

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

感謝這工作,你能解釋我什麼分區由titel意味着什麼? –

+0

https://www.postgresql.org/docs/9.3/static/tutorial-window.html'partition by'是'group by'的窗口類似物' –

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 
+0

謝謝,這個工作也對我更好理解 –