mysql - How can I SELECT rows from a table when I MAX(ColA) and GROUP BY ColB -
i found this question similar i'm still having troubles.
so start table named scores
id | player | time | scorea | scoreb | ~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~| 1 | john | 10 | 70 | 80 | 2 | bob | 22 | 75 | 85 | 3 | john | 52 | 55 | 75 | 4 | ted | 39 | 60 | 90 | 5 | john | 35 | 90 | 90 | 6 | bob | 27 | 65 | 85 | 7 | john | 33 | 60 | 80 |
i select best average score each player along information record. clarify, best average score highest value (scorea + scoreb)/2.
the results this
id | player | time | scorea | scoreb | avg_score | ~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~|~~~~~~~~~~~| 5 | john | 35 | 90 | 90 | 90 | 2 | bob | 22 | 75 | 85 | 80 | 4 | ted | 39 | 60 | 90 | 75 |
based on question linked above, tried query this,
select s.*, avg_score scores s inner join ( select max((scorea + scoreb)/2) avg_score, player, id scores group player ) avg_s on s.id = avg_s.id order avg_score desc, s.time asc
what gives me is,
id | player | time | scorea | scoreb | avg_score | ~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~|~~~~~~~~~~~| 1 | john | 10 | 70 | 80 | 90 | 2 | bob | 22 | 75 | 85 | 80 | 4 | ted | 39 | 60 | 90 | 75 |
as can see, has gotten correct max avg_score, record 5, gets rest of information record, record 1. missing? how ensure data comes same record? i'm getting correct avg_score want rest of data associated record, record 5 in case.
thanks in advance!
select x.* , (scorea+scoreb)/2 avg_score scores x join ( select player, max((scorea+scoreb)/2) max_avg_score scores group player) y on y.player = x.player , y.max_avg_score = (scorea+x.scoreb)/2;
Comments
Post a Comment