MySQL Forums
Forum List  »  Newbie

Re: sum top 5 question
Posted by: Peter Brawley
Date: May 03, 2016 09:23AM

If I understand your problem correctly, you forgot to select score values, and to order them in the outer query ...

SELECT tmp.lidnr, tmp.dicipline, tmp.score
FROM ( 
  SELECT 
    lidnr, dicipline, score,
    IF( @prev <> lidnr, @rownum := 1, @rownum := @rownum+1 ) AS rank, 
    @prev := lidnr 
  FROM scores t 
  JOIN (SELECT @rownum := NULL, @prev := 0) AS r 
  ORDER BY t.lidnr 
) AS tmp 
WHERE tmp.rank <=5 
ORDER BY lidnr asc, dicipline asc, score desc; 
+-------+-----------+-------+
| lidnr | dicipline | score |
+-------+-----------+-------+
|    10 | ZKG       |    99 |
|    10 | ZKG       |    88 |
|    10 | ZKG       |    75 |
|    10 | ZKG       |    75 |
|    10 | ZKG       |    66 |
|   220 | MKL       |    75 |
|   220 | MKL       |    65 |
|   220 | MKL       |    65 |
|   220 | MKL       |    45 |
|   220 | MKL       |    45 |
+-------+-----------+-------+

Options: ReplyQuote


Subject
Written By
Posted
April 28, 2016 04:34AM
April 28, 2016 01:28PM
April 29, 2016 08:33AM
May 02, 2016 02:33AM
May 02, 2016 09:58AM
May 03, 2016 07:05AM
Re: sum top 5 question
May 03, 2016 09:23AM
May 04, 2016 06:51AM
May 04, 2016 11:48AM
May 04, 2016 11:39PM
May 05, 2016 11:24AM
May 05, 2016 12:31PM
May 04, 2016 11:12PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.