I am trying to get the latest record from a table based on the time stamp. He is query I wrote:
SELECT DISTINCT
[Year],
[Type],
[Category],
[AnnualCost],
MAX([TimeStamp]) OVER (PARTITION BY [Year], [Type], [Category], [AnnualCost]) AS MaxTimeStamp
FROM
[PromOneSite].[Budgeting].[MISBasePrice]
WHERE
Year = 2016
AND category IN ('Leasing Office Desktop')
AND TimeStamp IS NOT NULL
Result:
Year Type Category AnnualCost MaxTimeStamp
----------------------------------------------------------------------------
2016 Equipment Hardware Location Leasing Office Desktop 750.00 2015-10-14 17:54:09.510
2016 Equipment Hardware Location Leasing Office Desktop 850.00 2015-10-14 17:54:20.630
I get these two records with different amounts and different timestamps. I know that it is because I put distinct in the query it brings me distinct Annualcost as well. But without the distinct I get about 30+ duplicate records.
How can just get only one record with the latest timestamp in this scenario.
Thanks in advance