How do I find the largest value in a column in postgres sql?

后端 未结 4 1669
南方客
南方客 2021-02-03 17:44

For example:

name | weight
jon    100    
jane   120    
joe    130

How do I only return the name of the person with the largest weight?

4条回答
  •  感动是毒
    2021-02-03 18:25

    If you need to find multiple rows, e.g. date on which each person had maximum weight:

    name | weight | day
    don    110      1
    don    120      20
    don    110      30
    joe    90       1
    joe    80       15
    joe    85       30
    

    i.e. for "don" you want to get "don | 120 | 20" and for joe you want "joe | 90 | 1", then you can write:

    SELECT name, max(weight), (array_agg(day ORDER BY weight DESC))[1] FROM tbl GROUP BY name
    

提交回复
热议问题