ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Selecting Nth MAX or MIN

Selecting Nth MAX or MIN

原创 Linux操作系统 作者:xhailiang 时间:2007-05-21 00:00:00 0 删除 编辑
This Select query will help you select the Nth Max or Min value from any table. For example, consider a table TAB1 in which you want to find the Nth Max or Min from the column, COL1. First, the query for Max:


SELECT * FROM  TAB1 a 

WHERE  &N = (SELECT count(DISTINCT(b.col1)) 
FROM TAB1 b WHERE a.col1<=b.col1)
Next, the query for Min: 

SELECT * FROM  TAB1 a 
WHERE &N = (SELECT count(DISTINCT(b.col1)) 
FROM TAB1 b WHERE a.col1>=b.col1)
If N=1 will return first max or first min. N=2 will return second max or min. 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/308563/viewspace-171983/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-02-17

  • 博文量
    270
  • 访问量
    414582