码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-30 15:27:50      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

采用分析函数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即可

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!