Sum Top 10 Values

北慕城南 提交于 2021-02-08 04:05:26

问题


I’ve searched and I know this has been asked before but I am struggling to get my head around what I can / can’t do.

My cycling club records race results each time a rider has entered a race. Each result is awarded points - 50 for 1st, 49 for 2nd etc.

So the table looks like

resultid(pk) | riderid(fk) | leaguepts
     1              1            50
     2              2            49
     3              3            48
     4              1            50
     5              2            42
     6              3            50
     7              4            30
...etc

I am trying to extract the sum of top 10 points awarded for each riderid from the results table.

(the actual database is a bit more complicated with a table for rider name / rider id and also a race table so we can display the results of each race etc but I just want to get the basic league table query working first of all)

So I want to extract the sum of the top 10 best scores for each rider. Then display each riders score, in a descending league table.

So far I’ve only had success using UNION ALL e.g.

SELECT sum(points) AS pts from
(
    SELECT points from `results`
    WHERE riderid = 1
    ORDER BY points DESC
    LIMIT 10
) as riderpts
UNION ALL
SELECT sum(points) AS pts from
(
    SELECT points from `results`
    WHERE riderid = 2
    ORDER BY points DESC
    LIMIT 10
) as riderpts
ORDER BY pts DESC

But there could be up to 90-odd riders who have registered at least one score so this query could get very big.

I found this which looks like it should work for me but doesn't. Sum top 5 values in MySQL I changed the column names for my table but it seems to sum all results, not the top 10 for each rider.

Alternatively I could just issue a query for each rider id. Not good I guess?

Subquerying is a problem because I can't limit on the inner query?

Run a job (manual or cron) to update the league table periodically and just display the table results?

Edit (not sure if this is the correct etiquette or I should start a new thread?). Gordon answered the question below but in the meantime I tried to work this out for myself using one of the links below. I could get results that returned the top 10 scores for each rider with the query below

set @riderid = '';
set @riderrow  = 1;

select riderid, leaguepts, row_number
from
(
    select
    riderid,
    leaguepts,
    @riderrow := if(@riderid = riderid, @riderrow + 1, 1) as row_number,
    @riderid := riderid as dummy
    from wp_tt_results order by riderid, leaguepts desc
) as x where x.row_number <= 10;

BUT I can't see what I would need to do next to get the sum of top 10 results per riderid?


回答1:


In MySQL, the easiest way to do this is probably to use variables:

SELECT riderid, sum(points)
FROM (SELECT r.*,
             (@rn := if(@r = riderid, @rn + 1,
                        if(@r := riderid, 1, 1)
                       )
             ) as seqnum
      FROM results r CROSS JOIN
           (SELECT @r := 0, @rn := 0) as wnw
      ORDER BY riderid, points DESC
     ) r
WHERE seqnum <= 10
GROUP BY riderid;


来源:https://stackoverflow.com/questions/28562294/sum-top-10-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!