Assuming seconds always are '00.000':
WITH cte AS ( SELECT Id,
DATEADD(mi, -DATEPART(mi, TSDest) % 5, TSDest) TSDest,
Value,
ROW_NUMBER() OVER (PARTITION BY DATEADD(mi, -DATEPART(mi, TSDest) % 5, TSDest)
ORDER BY TSDest ASC) rn
FROM test )
SELECT Id, TSDest, Value
FROM cte
WHERE rn = 1
ORDER BY TSDest
Added by OP
Thank you very much, this was very helpful. BTW, because I had records with seconds and milliseconds, I had to add cast the time to "smalldatetime" which did the trick very well,
WITH cte AS ( SELECT Id,
cast(DATEADD(mi, -DATEPART(mi, TSDest) % 5, TSDest) as smalldatetime) TSDest,
Value,
ROW_NUMBER() OVER (PARTITION BY cast(DATEADD(mi, -DATEPART(mi, TSDest) % 5, TSDest) as smalldatetime)
ORDER BY TSDest ASC) rn
FROM test )
SELECT Id, TSDest, Value
FROM cte
WHERE rn = 1
ORDER BY TSDest