2017-05-11 71 views
0

假設我有此表TOP 5分按月(每月爲一個varchar)

article | score | date (VARCHAR) 
A   10  2010/01 
B   10  2010/01 
C   10  2010/01 
D   10  2010/01 
E   10  2010/01 
F   1  2010/01 
G   1  2010/01 
H   1  2010/01 
B   9  2010/01 
B   9  2010/01 
A   4  2010/02 
C   10  2010/02 
G   10  2010/02 
G   2  2010/02 

,我會用不同的日期返回前5分。

的OUTPUT我喜歡:

article | average | date 
A   10  2010/01 
B   9.33  2010/01 
C   10  2010/01 
D   10  2010/01 
E   10  2010/01 
A   4  2010/02 
....  ...  ..... 

我嘗試用一​​些成績的一些疑問。

SELECT 
    article as articlex, 
    date as datex, 
    (SELECT avg(score) FROM test_anna WHERE article = articlex AND date = datex) as average 
FROM test_anna GROUP BY articlex, average ORDER BY datex ASC, articlex ASC 

如何使用mysql語句執行此操作? 謝謝!

+4

標記您的問題與您正在使用的數據庫。 –

回答

1

一種方法是使用row_number()rank()(取決於您要如何處理關係):

select t.* 
from (select t.*, 
      row_number() over (partition by date order by score desc) as seqnum 
     from test_anna t 
    ) t 
where seqnum <= 5;