且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据时间戳从 SQL Server 表中获取最新记录

更新时间:2023-01-29 21:01:18

select * from 
(  SELECT [Year]
         ,[Type]
         ,[Category]
         ,[AnnualCost]
         ,[TimeStamp] as MaxTimeStamp
         ,row_number() over (partition by [Year], [Type], [Category] order by [TimeStamp] desc ) as rn 
     FROM [PromOneSite].[Budgeting].[MISBasePrice]
    where Year = 2016
      and category IN ('Leasing Office Desktop')
      and TimeStamp IS Not Null 
) tt
where tt.rn = 1