Skip to main content
added 26 characters in body
Source Link
Akina
  • 20.3k
  • 2
  • 19
  • 22

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

fiddle


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

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

fiddle

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

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

fiddle


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
because I had records with seconds and milliseconds, I had to add cast the time to "smalldatetime" which did the trick very well
Source Link

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

fiddle

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

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

fiddle

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

fiddle

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
Source Link
Akina
  • 20.3k
  • 2
  • 19
  • 22

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

fiddle