postgre 中获取某个字段最小的另一个字段的记录

采用分析函数row_number()

select * from 

(

  select a.*,row_number() over (partition by column1 order by column2 [desc]) as rn

  from table1 

) q

where rn = 1

其中,partition by 是根据column1字段来分组,再根据column2来排序(默认为升序),最终的结果会给一个排序行号row_number 

取rn = 1为column2字段值最小的记录

若要取最大的,则order by column2 desc,然后取rn = 1即可

原文地址:https://www.cnblogs.com/ztgentle/p/4262054.html