I have a (mssql) ranking list table which has an id , a username , a date and a score attribute.
id | username | date | score
----------------------------
1 joe 01/2020 200
2 bob 01/2020 300
3 max 02/2020 350
4 jane 02/2020 300
5 bob 02/2020 250
6 joe 03/2020 150
What I'm trying to achieve is to select the highest score grouped by date with the corresponding id and username, which would produce a result like the following:
id | username | date | score
----------------------------
2 bob 01/2020 300
3 max 02/2020 350
6 joe 03/2020 150
I'm by no means a database guy and I failed miserably!
Can someone help me with this? Thanks!
Edit: I don't care about ties at this point.
if your database supports window functions:
Select * from (Select t.*,
row_number() OVER (Partition by date order by score desc) rn
from table t) where rn = 1;
+----+----------+---------+-------+----+
| id | username | dte | score | rn |
+----+----------+---------+-------+----+
| 2 | bob | 01/2020 | 300 | 1 |
+----+----------+---------+-------+----+
| 3 | max | 02/2020 | 350 | 1 |
+----+----------+---------+-------+----+
| 6 | joe | 03/2020 | 150 | 1 |
+----+----------+---------+-------+----+
Try like below
select a.* form table_name a
where a.score=( select max(score) from table_name b where a.date=b.date )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.