Search code examples
mysqlsqlwhere-clausegreatest-n-per-group

Is this a case where window function should be used?


I have a table that has user ids, teams, zips, and a metric:

+------+------+--------+--------+
|  id  | team |  zip   | metric |
+------+------+--------+--------+
| 0001 | t1   | 000000 |    0.0 |
| 0001 | t2   | 000001 |    1.0 |
| 0002 | t1   | 000000 |    0.0 |
| 0002 | t1   | 000001 |    1.0 |
| 0003 | t2   | 000000 |    0.0 |
| 0003 | t2   | 000001 |    0.0 |
+------+------+--------+--------+

I need only the rows that have the highest metric for each zip, team, and id. If two zips have the same metric, pick one at random:

+------+------+--------+--------+
|  id  | team |  zip   | metric |
+------+------+--------+--------+
| 0001 | t1   | 000000 |    0.0 |
| 0001 | t2   | 000001 |    1.0 |
| 0002 | t1   | 000001 |    1.0 |
| 0003 | t2   | 000000 |    0.0 |
+------+------+--------+--------+

Solution

  • One option uses row_number():

    select *
    from (
        select t.*, row_number() over(partition by zip, team, id order by metric desc) rn 
        from mytable t
    ) t
    where rn = 1