I have a table in SQL Server 2016 with the following data:
id | t | memory
-------+-------------------------+------------
620255 | 2017-07-17 16:11:25.100 | 11632640
620127 | 2017-07-17 16:11:24.100 | 11632640
619999 | 2017-07-17 16:11:23.097 | 11632640
619872 | 2017-07-17 16:11:22.097 | 11632640
I am trying to get the average of the memory
per second. So in order to do that I tried the below query:
SELECT
AVG(memory) AS avgMemory,
DATEADD(second, DATEDIFF(second, 0, t), 0) AS t
FROM
Table1 AS Table1
WHERE
t BETWEEN '2017-07-17 16:11:00.000' AND '2017-07-17 16:12:00.000'
GROUP BY
DATEADD(second, DATEDIFF(second, 0, t), 0)
ORDER BY
t ASC;
But upon execution, I get the following error:
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
As per the official docs and this SO post, the maximum difference for seconds can be 68 years, but in my case it is just 60 seconds.
So why am I getting this error? And what can I do to resolve it?
Here is the SQLFiddle with the mentioned issue: Fiddle