假设我有这张表
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
我会以不同的日期返回TOP 5分数。
我喜欢的输出:
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语句执行此操作? 谢谢!
答案 0 :(得分: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;