191

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

列中可能存在重复值。

4

46 回答 46

346
SELECT MAX( col )
  FROM table
 WHERE col < ( SELECT MAX( col )
                 FROM table )
于 2008-08-28T12:43:40.563 回答
63
SELECT MAX(col) 
FROM table 
WHERE col NOT IN ( SELECT MAX(col) 
                   FROM table
                 );
于 2011-09-09T13:13:56.907 回答
32

在 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这是因为与聚合使用的表或索引扫描相比,排序操作相对较慢。

或者,在 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
于 2008-08-28T13:19:30.587 回答
25

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

我还看到了一些不起作用的解决方案,因为它们没有考虑到重复的可能性,所以要小心你接受哪些。最后,我看到一些可行的方法,但会对表进行两次完整扫描。您要确保第二次扫描只查看 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 [column] 
FROM [Table] 
GROUP BY [column] 
ORDER BY [column] DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
于 2008-08-28T14:24:04.143 回答
17

我想你可以这样做:

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

或者

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

取决于您的数据库服务器。提示:SQL Server 不做 LIMIT。

于 2008-08-28T12:43:25.990 回答
7

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

SELECT DISTINCT value 
FROM Table 
ORDER BY value DESC 
LIMIT 2

但是,如果您必须使用 SQL 选择第二个值,那么如何:

SELECT MIN(value) 
FROM ( SELECT DISTINCT value 
       FROM Table 
       ORDER BY value DESC 
       LIMIT 2
     ) AS t
于 2008-08-28T12:42:49.207 回答
7

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

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

您可以在以下链接中找到更多详细信息

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

于 2013-01-23T06:15:57.210 回答
6

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

SELECT `Column` 
FROM `Table` 
ORDER BY `Column` DESC 
LIMIT 1,1;
于 2012-10-25T11:01:20.513 回答
6

微软SQL

SELECT  *
  FROM [Users]
    order by UserId desc OFFSET 1 ROW 
FETCH NEXT 1 ROW ONLY;

MySQL

SELECT  *
  FROM Users
    order by UserId desc LIMIT 1 OFFSET 1

不需要子查询...只需跳过一行并按降序选择第二行

于 2017-07-12T05:34:39.713 回答
4
SELECT MAX(Salary) 
FROM Employee 
WHERE Salary NOT IN ( SELECT MAX(Salary) 
                      FROM Employee 
                    )

此查询将从结果中返回最高薪水——其中不包含整个表中的最高薪水。

于 2015-05-09T02:40:18.247 回答
3

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

 SELECT TOP 1 LEAD(MAX (column)) OVER (ORDER BY column desc)
 FROM TABLE 
 GROUP BY column
于 2016-07-29T18:52:25.463 回答
3

这是非常简单的代码,你可以试试这个:-

例如:表名 = 测试

salary 

1000
1500
1450
7500

获得第二大值的 MSSQL 代码

select salary from test order by salary desc offset 1 rows fetch next 1 rows only;

这里“偏移 1 行”表示表的第 2 行,“仅获取下 1 行”仅用于显示该 1 行。如果您不使用“仅获取下 1 行”,那么它会显示第二行中的所有行。

于 2017-06-23T07:46:07.310 回答
2

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

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

于 2012-06-27T19:04:40.010 回答
2

使用此查询。

SELECT MAX( colname ) 
FROM Tablename 
where colname < (
    SELECT MAX( colname ) 
    FROM Tablename)
于 2014-03-24T10:44:48.517 回答
2

最简单的

select sal 
from salary 
order by sal desc 
limit 1 offset 1
于 2015-09-10T08:08:03.173 回答
1

select max([COLUMN_NAME]) from [TABLE_NAME]Tom,相信当section 中返回多个值时,这将失败。即数据集中有两个以上的值。

对您的查询稍作修改即可 -

select max([COLUMN_NAME]) 
from [TABLE_NAME] 
where [COLUMN_NAME] IN ( select max([COLUMN_NAME]) 
                         from [TABLE_NAME] 
                       )
于 2010-11-25T16:53:19.267 回答
1
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
                                      )
                   );

子查询返回除最大值之外的所有值。从返回的列表中选择最大值。

于 2010-11-25T16:58:29.250 回答
1
select min(sal) from emp where sal in 
    (select TOP 2 (sal) from emp order by sal desc)

笔记

sal 是列名
emp 是表名

于 2011-08-06T13:17:27.063 回答
1
select col_name
from (
    select dense_rank() over (order by col_name desc) as 'rank', col_name
    from table_name ) withrank 
where rank = 2
于 2012-03-30T08:40:24.693 回答
1
SELECT 
    * 
FROM 
    table 
WHERE 
    column < (SELECT max(columnq) FROM table) 
ORDER BY 
    column DESC LIMIT 1
于 2012-10-23T16:13:39.423 回答
1

这是另一种查找列的第二大值的方法。考虑表'Student'和列'Age'。然后查询是,

select top 1 Age 
from Student 
where Age in ( select distinct top 2 Age  
               from Student order by Age desc 
             ) order by Age asc
于 2013-08-15T08:07:54.277 回答
1

这是最简单的方法:

SELECT
      Column name
FROM
      Table name 
ORDER BY 
      Column name DESC
LIMIT 1,1
于 2013-08-23T18:28:41.323 回答
1
select age 
from student 
group by id having age< ( select max(age) 
                          from student 
                        )
order by age 
limit 1
于 2015-02-28T18:16:43.817 回答
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 的第二部分 = 多少值

于 2016-12-06T16:03:12.227 回答
1
SELECT MAX(sal) 
FROM emp
WHERE sal NOT IN ( SELECT top 3 sal 
                   FROM emp order by sal desc 
                 )
            

这将返回emp表的第三高sal

于 2017-03-10T11:43:16.893 回答
1
select max(column_name) 
from table_name
where column_name not in ( select max(column_name) 
                           from table_name
                         );

not in是排除 column_name 最大值的条件。

参考:程序员面试

于 2017-09-25T14:34:58.133 回答
0
select top 1 MyIntColumn from MyTable
where
 MyIntColumn <> (select top 1 MyIntColumn from MyTable order by MyIntColumn desc)
order by MyIntColumn desc
于 2008-08-28T12:44:49.513 回答
0

这适用于 MS SQL:

select max([COLUMN_NAME]) from [TABLE_NAME] where [COLUMN_NAME] < 
 ( select max([COLUMN_NAME]) from [TABLE_NAME] )
于 2008-08-28T12:45:03.383 回答
0

像这样的东西?不过,我还没有测试过:

select top 1 x
from (
  select top 2 distinct x 
  from y 
  order by x desc
) z
order by x
于 2008-08-28T12:45:30.260 回答
0

请参阅如何选择 SQL 数据库表中的第 n 行?.

Sybase SQL Anywhere 支持:

SELECT TOP 1 START AT 2 value from table ORDER BY value
于 2008-08-28T13:36:14.773 回答
0
select * from emp e where 3>=(select count(distinct salary)
    from emp where s.salary<=salary)

此查询选择最多三个薪水。如果两个 emp 获得相同的薪水,这不会影响查询。

于 2009-07-09T05:03:36.937 回答
0

使用相关查询:

Select * from x x1 where 1 = (select count(*) from x where x1.a < a)
于 2009-09-14T20:21:11.537 回答
0

查询以查找连续第二高的数字-

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 23或者4您可以分别找到第 3、第 4 和第 5 高。

于 2013-03-25T06:37:38.823 回答
0

我们还可以使用 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 
于 2013-04-23T10:54:26.287 回答
0
SELECT * FROM EMP
WHERE salary=
        (SELECT MAX(salary) FROM EMP
           WHERE salary != (SELECT MAX(salary) FROM EMP)
        );
于 2013-05-13T07:17:17.497 回答
0

尝试:

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
于 2013-09-12T11:40:58.863 回答
0
select * from [table] where (column)=(select max(column)from [table] where column < (select max(column)from [table]))
于 2013-11-26T07:43:51.500 回答
0
select MAX(salary) as SecondMax from test where salary !=(select MAX(salary) from test)
于 2014-04-21T12:20:18.527 回答
0
select score 
from table 
where score = (select max(score)-1 from table)
于 2016-03-04T03:57:52.270 回答
0

Microsoft SQL Server - 对第 N 个最高值使用两个 TOP(别名子查询)。

要解决第二高:

SELECT TOP 1 q.* 
FROM (SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC) as q
ORDER BY column_name ASC;

使用 TOP 两次,但需要一个别名子查询。本质上,内部查询以降序获取最大的 2 个值,然后外部查询以升序翻转,因此第二高的现在位于顶部。SELECT 语句返回此顶部。

要解决第 n 个最高值,请修改子查询 TOP 值。例如:

SELECT TOP 1 q.* 
FROM (SELECT TOP 5 column_name FROM table_name ORDER BY column_name DESC) as q
ORDER BY column_name;

将返回第 5 个最高值。

于 2016-05-23T20:11:48.087 回答
0
select extension from [dbo].[Employees] order by extension desc offset  2  rows fetch next  1 rows only
于 2016-09-03T06:47:26.930 回答
0

非常简单。distinct 关键字也会处理重复项。

SELECT distinct SupplierID FROM [Products] order by SupplierID desc limit 1 offset 1
于 2017-04-17T13:42:47.950 回答
0

从 SQL 表中获取倒数第二行的最简单方法是使用和设置.ORDER BYColumnNameDESCLIMIT 1,1

试试这个:

SELECT * from `TableName` ORDER BY `ColumnName` DESC LIMIT 1,1
于 2017-06-06T07:14:03.680 回答
0
 SELECT  * FROM `employee` WHERE  employee_salary = (SELECT employee_salary 
 FROM`employee` GROUP BY employee_salary ORDER BY employee_salary DESC LIMIT 
 1,1)
于 2017-09-25T11:32:40.600 回答
0

您可以使用以下查询找到第 n 个最高值。

    select top 1 UnitPrice from (select distinct top n UnitPrice from 
[Order Details] order by UnitPrice desc) as Result order by UnitPrice asc

在这里,n 的值将是 1(最高数)、2(第二高数)、3(第三高数)...

于 2018-02-24T15:46:40.080 回答
0

首先制作一个没有最高薪水的虚拟表,然后从虚拟表中查询最大值

SELECT max(salary) from (Select * FROM emp WHERE salary<> (SELECT MAX(salary) from emp)) temp
于 2018-11-15T20:04:51.200 回答