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

Mau*_*rez 4 sql sql-server

我有以下MS SQL查询

select top 10 RegisterTime
from  orderNotifications
order by RegisterTime asc
Run Code Online (Sandbox Code Playgroud)

如何获得该RegisterTime查询的最大值?

我试过这个

select max(RegisterTime) in (
    select top 10 RegisterTime
    from  orderNotifications
    order by RegisterTime asc
)
Run Code Online (Sandbox Code Playgroud)

但我得到了

消息156,级别15,状态1,行1关键字'in'附近的语法不正确.

消息156,级别15,状态1,行4关键字'order'附近的语法不正确.

Har*_* CO 9

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

SELECT MAX(RegisterTime)
FROM (SELECT TOP 10 RegisterTime
      FROM orderNotifications
      ORDER BY RegisterTime
      )sub
Run Code Online (Sandbox Code Playgroud)

  • @MauricioGracia'sub'是该问题的评论中所述的别名.这是必需的,因为括号中的子查询本质上是查询中生成的临时表,要访问它,您需要对其进行命名. (2认同)