sql - 从 TOP 数量的记录中获取 MAX 值

标签 sql sql-server

我有以下 MS SQL 查询

select top 10 RegisterTime
from  orderNotifications
order by RegisterTime asc

如何获取该查询的最大RegisterTime

我已经尝试过了

select max(RegisterTime) in (
    select top 10 RegisterTime
    from  orderNotifications
    order by RegisterTime asc
)

但是我得到了

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'in'.

Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'order'.

最佳答案

使您的 TOP 10 查询成为子查询:

SELECT MAX(RegisterTime)
FROM (SELECT TOP 10 RegisterTime
      FROM orderNotifications
      ORDER BY RegisterTime
      )sub

关于sql - 从 TOP 数量的记录中获取 MAX 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20691189/

相关文章:

php - 查询-未得到正确答案

mysql - 使用 mysql LIKE 在列内搜索多次

mysql - 如何从 SQL 子查询返回数组?

java - 是否可以使用 sql server 2000 和 java 1.4 将 arraylist 对象传递给 sql 过程?

mysql - 不确定 SQL Select 查询

sql-server - SQL Server 可空列

sql-server - T-SQL 中多个字段中的相同窗口函数 : optimization?

sql - 在 SQL Server 中解析 JSON

sql - 在恒定时间内保留没有重复项的随机项目

php - 在代码中执行时更新查询不更新