| |
comp.databases.mysql |
On Feb 26, 4:39 am, "howa" <howac...@gmail.com> wrote: > user_id score > I want to select te average score of a user and will use sth > SELECT avg( score ) > but sometimes, i want to select the average, but dropping the lowest N > thanks SELECT count( * ) rank, a.user_id, A.score
> 1 90
> 1 80
> 1 70
> 2 85
> 2 80
> 2 80
> .
> .
> like:
> FROM `score`
> GROUP BY user_id
> score, how
> to write the query?
primary key 'record_id' to the table to deal with ties.
FROM score A
JOIN score B ON ( A.user_id = B.user_id
AND A.score = B.score
AND A.record_id <= B.record_id )
OR (
A.user_id = B.user_id
AND A.score < B.score
)
GROUP BY A.user_id, A.record_id
HAVING rank <=2
LIMIT 0 , 30