You can use a CTE (Common Table Expression) and the ranking function - something like this:
;WITH YourQuery AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY RecordId ORDER BY InsertDate DESC) 'RowNumber'
FROM dbo.YourTable
WHERE InsertDate < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
)
SELECT *
FROM YourQuery
WHERE RowNumber = 1
The CTE (the inner SELECT) selects all rows, partitions them by RecordId
, orders them by InsertDate
descending (newest first) - so this gives you a list of all records, ordered by ID, InsertDate, and a RowNumber
field which starts at 1 for each new RecordId
.
So for each RecordId
, the entry with RowNumber = 1
is the most recent one - that's what the second (outer) SELECT is doing.