Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAX value from a TOP number of records

Tags:

sql

sql-server

I have the following MS SQL Query

select top 10 RegisterTime
from  orderNotifications
order by RegisterTime asc

How can I get the max RegisterTime of that query ?

I have tried this

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

But I am getting

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'.

like image 806
Mauricio Gracia Gutierrez Avatar asked Jan 11 '23 15:01

Mauricio Gracia Gutierrez


1 Answers

Make your TOP 10 query a subquery:

SELECT MAX(RegisterTime)
FROM (SELECT TOP 10 RegisterTime
      FROM orderNotifications
      ORDER BY RegisterTime
      )sub
like image 162
Hart CO Avatar answered Jan 17 '23 14:01

Hart CO