什么是最简单的SQL查询find第二大的价值?

什么是最简单的SQL查询来查找特定列中的第二大整数值?

列中可能有重复的值。

SELECT MAX( col ) FROM table WHERE col < ( SELECT MAX( col ) FROM table ) 
 SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table); 

在T-SQL中有两种方法:

 --filter out the max select max( col ) from [table] where col < ( select max( col ) from [table] ) --sort top two then bottom one select top 1 col from ( select top 2 col from [table] order by col) topTwo order by col desc 

在Microsoft SQL中,第一种方法比第二种方法快两倍,即使所讨论的列是聚集的。

这是因为与max聚合使用的表或索引扫描相比,sorting操作相对较慢。

或者,在Microsoft SQL 2005及更高版本中,您可以使用ROW_NUMBER()函数:

 select col from ( select ROW_NUMBER() over (order by col asc) as 'rowNum', col from [table] ) withRowNum where rowNum = 2 

我在这里看到了一些SQL Server特定的和一些MySQL特定的解决scheme,所以你可能想弄清楚你需要哪个数据库。 虽然如果我不得不猜测我会说SQL Server,因为这在MySQL中是微不足道的。

我也看到一些解决scheme不能工作,因为他们没有考虑到重复的可能性,所以要小心哪些是你接受的。 最后,我看到了一些可以工作但是会对表格进行两次完整扫描的工具。 你想确保第二次扫描只看2个值。

SQL Server(2012之前):

 SELECT MIN([column]) AS [column] FROM ( SELECT TOP 2 [column] FROM [Table] GROUP BY [column] ORDER BY [column] DESC ) a 

MySQL的:

 SELECT `column` FROM `table` GROUP BY `column` ORDER BY `column` DESC LIMIT 1,1 

更新:

SQL Server 2012现在支持更清晰(和标准 )的OFFSET / FETCH语法:

 SELECT TOP 2 [column] FROM [Table] GROUP BY [column] ORDER BY [column] DESC OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY; 

我想你可以做这样的事情:

 SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT 1 OFFSET 1 

要么

 SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT (1, 1) 

取决于你的数据库服务器。 提示:SQL Server不会执行LIMIT。

最简单的方法是从应用程序中的结果集中获取第二个值:

 SELECT DISTINCT value FROM Table ORDER BY value DESC LIMIT 2 

但是,如果您必须使用SQLselect第二个值,那么:

 SELECT MIN(value) FROM (SELECT DISTINCT value FROM Table ORDER BY value DESC LIMIT 2) AS t 

您可以使用以下查询find列的第二大值

 SELECT * FROM TableName a WHERE 2 = (SELECT count(DISTINCT(b.ColumnName)) FROM TableName b WHERE a.ColumnName <= b.ColumnName); 

你可以在下面的链接find更多的细节

http://www.abhishekbpatel.com/2012/12/how-to-get-nth-maximum-and-minimun.html

一个非常简单的查询来查找第二大的值

 SELECT `Column` FROM `Table` ORDER BY `Column` DESC LIMIT 1,1; 
 SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee ) 

该查询将返回结果中的最高工资 – 不包含总表中的最高工资。

 select * from (select ROW_NUMBER() over (Order by Col_x desc) as Row, Col_1 from table_1)as table_new tn inner join table_1 t1 on tn.col_1 = t1.col_1 where row = 2 

希望这有助于获得任何行的价值…..

最简单的

 select sal from salary order by sal desc limit 1 offset 1 

老问题我知道,但这给了我一个更好的执行计划:

  SELECT TOP 1 LEAD(MAX (column)) OVER (ORDER BY column desc) FROM TABLE GROUP BY column 
 select min(sal) from emp where sal in (select TOP 2 (sal) from emp order by sal desc) 

注意

sal是col的名字
emp是表名

Tom,相信在select max([COLUMN_NAME]) from [TABLE_NAME]部分的select max([COLUMN_NAME]) from [TABLE_NAME]返回多个值时会失败。 即数据集中有多于2个值的地方。

稍微修改您的查询将工作 –

 select max([COLUMN_NAME]) from [TABLE_NAME] where [COLUMN_NAME] **IN** ( select max([COLUMN_NAME]) from [TABLE_NAME] ) 
 select max(COL_NAME) from TABLE_NAME where COL_NAME in (select COL_NAME from TABLE_NAME where COL_NAME < (select max(COL_NAME) from TABLE_NAME)); 

子查询返回除最大值以外的所有值。 从返回的列表中select最大值。

 select col_name from ( select dense_rank() over (order by col_name desc) as 'rank', col_name from table_name ) withrank where rank = 2 
 SELECT * FROM table WHERE column < (SELECT max(columnq) FROM table) ORDER BY column DESC LIMIT 1 

这是最狡猾的方式:

 SELECT Column name FROM Table name ORDER BY Column name DESC LIMIT 1,1 

正如你提到的重复值。 在这种情况下,您可以使用DISTINCTGROUP BY来查找第二高的值

这是一张桌子

薪水

在这里输入图像描述

通过…分组

 SELECT amount FROM salary GROUP by amount ORDER BY amount DESC LIMIT 1 , 1 

不同

 SELECT DISTINCT amount FROM salary ORDER BY amount DESC LIMIT 1 , 1 

LIMIT =起始索引的第一部分

LIMIT的第二部分=多less个值

像这样的东西? 虽然我没有testing过,

 select top 1 x from ( select top 2 distinct x from y order by x desc ) z order by x 

请参阅如何selectSQL数据库表中的第n行? 。

Sybase SQL Anywhere支持:

 SELECT TOP 1 START AT 2 value from table ORDER BY value 

使用相关查询:

 Select * from x x1 where 1 = (select count(*) from x where x1.a < a) 
 select * from emp e where 3>=(select count(distinct salary) from emp where s.salary<=salary) 

该查询select最多三个工资。 如果两个emp获得相同的薪水,这不会影响查询。

 select top 1 MyIntColumn from MyTable where MyIntColumn <> (select top 1 MyIntColumn from MyTable order by MyIntColumn desc) order by MyIntColumn desc 

这在MS SQL中工作:

 select max([COLUMN_NAME]) from [TABLE_NAME] where [COLUMN_NAME] < ( select max([COLUMN_NAME]) from [TABLE_NAME] ) 

查询查找行中第二个最高的数字 –

 select Top 1 (salary) from XYZ where Salary not in (select distinct TOP 1(salary) from XYZ order by Salary desc) ORDER BY Salary DESC 

通过改变突出显示的Top 1TOP 24你可以find第三,第四和第五最高分别。

我们也可以使用order by和top 1元素,如下所示:

 Select top 1 col_name from table_name where col_name < (Select top 1 col_name from table_name order by col_name desc) order by col_name desc 
 SELECT * FROM EMP WHERE salary= (SELECT MAX(salary) FROM EMP WHERE salary != (SELECT MAX(salary) FROM EMP) ); 

这是查找列的第二大值的另一种方式。考虑表格'学生'和列'年龄'。然后查询是,

 select top 1 Age from Student where Age in(select distinct top 2 Age from Student order by Age desc) order by Age asc 

尝试:

 select a.* ,b.* from (select * from (select ROW_NUMBER() OVER(ORDER BY fc_amount desc) SrNo1, fc_amount as amount1 From entry group by fc_amount) tbl where tbl.SrNo1 = 2) a , (select * from (select ROW_NUMBER() OVER(ORDER BY fc_amount asc) SrNo2, fc_amount as amount2 From entry group by fc_amount) tbl where tbl.SrNo2 =2) b