在特定列中查找第二大整数值的最简单 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
这是因为与聚合使用的表或索引扫描相比,排序操作相对较慢。
或者,在 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 的解决方案,因此您可能想澄清您需要哪个数据库。虽然如果我不得不猜测我会说 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;
我想你可以这样做:
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
但是,如果您必须使用 SQL 选择第二个值,那么如何:
SELECT MIN(value)
FROM ( SELECT DISTINCT value
FROM Table
ORDER BY value DESC
LIMIT 2
) AS t
您可以使用以下查询找到列的第二大值
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
查找第二大值的非常简单的查询
SELECT `Column`
FROM `Table`
ORDER BY `Column` DESC
LIMIT 1,1;
微软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
不需要子查询...只需跳过一行并按降序选择第二行
SELECT MAX(Salary)
FROM Employee
WHERE Salary NOT IN ( SELECT MAX(Salary)
FROM Employee
)
此查询将从结果中返回最高薪水——其中不包含整个表中的最高薪水。
我知道的老问题,但这给了我一个更好的执行计划:
SELECT TOP 1 LEAD(MAX (column)) OVER (ORDER BY column desc)
FROM TABLE
GROUP BY column
这是非常简单的代码,你可以试试这个:-
例如:表名 = 测试
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 行”,那么它会显示第二行中的所有行。
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 MAX( colname )
FROM Tablename
where colname < (
SELECT MAX( colname )
FROM Tablename)
最简单的
select sal
from salary
order by sal desc
limit 1 offset 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]
)
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 min(sal) from emp where sal in
(select TOP 2 (sal) from emp order by sal desc)
笔记
sal 是列名
emp 是表名
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
这是另一种查找列的第二大值的方法。考虑表'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
这是最简单的方法:
SELECT
Column name
FROM
Table name
ORDER BY
Column name DESC
LIMIT 1,1
select age
from student
group by id having age< ( select max(age)
from student
)
order by age
limit 1
SELECT MAX(sal)
FROM emp
WHERE sal NOT IN ( SELECT top 3 sal
FROM emp order by sal desc
)
这将返回emp表的第三高sal
select max(column_name)
from table_name
where column_name not in ( select max(column_name)
from table_name
);
not in是排除 column_name 最大值的条件。
参考:程序员面试
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 x
from (
select top 2 distinct x
from y
order by x desc
) z
order by x
Sybase SQL Anywhere 支持:
SELECT TOP 1 START AT 2 value from table ORDER BY value
select * from emp e where 3>=(select count(distinct salary)
from emp where s.salary<=salary)
此查询选择最多三个薪水。如果两个 emp 获得相同的薪水,这不会影响查询。
使用相关查询:
Select * from x x1 where 1 = (select count(*) from x where x1.a < a)
查询以查找连续第二高的数字-
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 1
为TOP 2
, 3
或者4
您可以分别找到第 3、第 4 和第 5 高。
我们还可以使用 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 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
select * from [table] where (column)=(select max(column)from [table] where column < (select max(column)from [table]))
select MAX(salary) as SecondMax from test where salary !=(select MAX(salary) from test)
select score
from table
where score = (select max(score)-1 from table)
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 个最高值。
select extension from [dbo].[Employees] order by extension desc offset 2 rows fetch next 1 rows only
非常简单。distinct 关键字也会处理重复项。
SELECT distinct SupplierID FROM [Products] order by SupplierID desc limit 1 offset 1
从 SQL 表中获取倒数第二行的最简单方法是使用和设置.ORDER BY
ColumnName
DESC
LIMIT 1,1
试试这个:
SELECT * from `TableName` ORDER BY `ColumnName` DESC LIMIT 1,1
SELECT * FROM `employee` WHERE employee_salary = (SELECT employee_salary
FROM`employee` GROUP BY employee_salary ORDER BY employee_salary DESC LIMIT
1,1)
您可以使用以下查询找到第 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(第三高数)...
首先制作一个没有最高薪水的虚拟表,然后从虚拟表中查询最大值
SELECT max(salary) from (Select * FROM emp WHERE salary<> (SELECT MAX(salary) from emp)) temp