Is there any SQL query for sort the latest date with time from the database

Viewed 42

Is there any SQL query for sort latest date and time from the database. Example below

enter image description here

Output:

enter image description here

1 Answers

You can use SQL window functions to order and mark records by partition. So using the row_number function you can mark the first row by date as row number 1 and then filter for that.

select [Device name], [Reporting Time], [Unique Id]
  from (select *, 
              row_number() 
                  over (partition by [Device name]      
                  order by [Reporting Time] asc) as [row number]
         from [Your Table Name]
        ) as a
 where a.[row number] = 1
Related