php - mySQL sum multiple rows and rank handling ties -


i have following table:

  userid  compid round score    bonus   2980    3     0     50          0   50        3     0     80          0   52        3     0     80          0   55        3     0     20          0   58        3     0     100         0   106      3     0     120         0   555      3     0     50          0   100     3     0     30          0   50        3     1     90          0   52        3     1     50          0   106      3     1     30          0 

i want able not sum score , bonus cols per user rounds, rank them accordingly. should able handle ties sequence 1,2,2,4,5,5,5,8:

trick don't want create temp table or insert ranking table. also, can not limit return - need rank entire table regardless of rounds completed.

i have managed rank rows can't seem group them sum. , of course have managed sum separately - need 'join' 2 steps.

obtaining sums per user:

select userid,sum(score+bonus) tscore entries  group userid order tscore desc  userid  tscore 50      170 106    150 52      130 58      100 2980      50 555    50 100    30 55        20 

rank without score sum : (thanks mysql cookbook)

select @rownum := @rownum + 1 row, @rank := if(@prev_val!=score,@rownum,@rank) rank, userid, @prev_val := score score exodus_entries order score desc  row   rank  userid      score 1      1    106         120 2      2    58          100 3      3    50          90 4      4    50          80 5      4    52          80 6      6    52          50 7      6    555         50 8      6    2980        50 9      9    100         30 10    9     106         30 11    11    55          20 

thanks time , help! advice welcome!

select @rownum := @rownum + 1 row,     @rank := if(@prev_val!=score,@rownum,@rank) rank,     userid,     @prev_val := score score (     select userid, sum(score+bonus) score      entries      group userid  ) exodus_entries  order score desc 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -