Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Select X of of Y in Aggregation
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
strawberry  
View profile   Translate to Translated (View Original)
 More options 26 Feb 2007, 13:51
Newsgroups: comp.databases.mysql
From: "strawberry" <zac.ca...@gmail.com>
Date: 26 Feb 2007 05:51:31 -0800
Local: Mon 26 Feb 2007 13:51
Subject: Re: Select X of of Y in Aggregation
On Feb 26, 4:39 am, "howa" <howac...@gmail.com> wrote:

> Consider a table like:

> user_id      score
>       1      90
>       1      80
>       1      70
>       2      85
>       2      80
>       2      80
>       .
>       .

> I want to select te average score of a user and will use sth
> like:

> SELECT avg( score )
> FROM `score`
> GROUP BY user_id

> but sometimes, i want to select the average, but dropping the lowest N
> score, how
> to write the query?

> thanks

Well here's a pretty substantial clue. Note that I've added the
primary key 'record_id' to the table to deal with ties.

SELECT count( * ) rank, a.user_id, A.score
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google