Bluehost.com Web Hosting $6.95

sum the top three

This is a discussion on sum the top three within the MySQL Database forums, part of the Database Forums category; Dear All, I hope this is the correct group to ask the following question. I have a table called results ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-30-2007
Geoff
 
Posts: n/a
Default sum the top three

Dear All,

I hope this is the correct group to ask the following question.

I have a table called results which has entries of the form:

date ; name ; score.

I want a query something like the following ...

SELECT name, sumTopThree(score) as total
FROM results
GROUP BY name
ORDER BY total DESC

Clearly what I need is to define the function sumTopThree which is
intended to be the sum of the top three results per player. Is there
any way to do this?

Thankyou in advance,

Geoff Ostrin.

Reply With Quote
  #2 (permalink)  
Old 01-30-2007
strawberry
 
Posts: n/a
Default Re: sum the top three

Hmm, I wonder if CP will be along in a minute ;-)

Anyway, see if you can adapt this query to suit your requirements...

SELECT lastname, sum(score) totals from (
select p1.lastname, p1.firstname, score, rank
from people p1
join
(select count(*) rank,
a.lastname, a.firstname
from people A join
people B
on (A.lastname = B.lastname
and A.score = B.score
and A.firstname <= B.firstname)
or (A.lastname = B.lastname
and A.score < B.score)
group by A.lastname, A.firstname) N
on p1.lastname= N.lastname and
p1.firstname = N.firstname
order by p1.lastname, rank) xxx
where rank <= 3
group by lastname
order by totals desc, lastname;

On 30 Jan, 13:54, "Geoff" <geoffost...@yahoo.co.uk> wrote:
> Dear All,
>
> I hope this is the correct group to ask the following question.
>
> I have a table called results which has entries of the form:
>
> date ; name ; score.
>
> I want a query something like the following ...
>
> SELECT name, sumTopThree(score) as total
> FROM results
> GROUP BY name
> ORDER BY total DESC
>
> Clearly what I need is to define the function sumTopThree which is
> intended to be the sum of the top three results per player. Is there
> any way to do this?
>
> Thankyou in advance,
>
> Geoff Ostrin.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:52 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0