I thought this would be simple, but I have a problem.
SELECT DATEDIFF (s,getdate(), max(TimeCreated)) as difference
FROM Ens.MessageHeader
My expectation was that this would show the difference between the current time and the timestamp of the most recent record. If I execute it, I get something lik e 15057
which could possibly be right. However, the next time I execute it, it is lower. There haven't been any new records. Right now it's at 15024
. Still no new records. The most recent timestamp is currently 2016-05-13 08:51:16
The idea is that I can watch this counter and when it resets to zero I know there is a new message.
What am I missing?
Update I added some additional data to the query to help see what is going on:
SELECT getDate() as now, max(TimeCreated) as latest, DATEDIFF (s,getdate(), max(TimeCreated)) as difference
FROM Ens.MessageHeader
Here are a couple result sets:
now latest difference
------------------- ------------------- ----------
2016-05-13 09:50:45 2016-05-13 08:51:16 14431
2016-05-13 09:52:29 2016-05-13 08:51:16 14327
2016-05-13 09:52:50 2016-05-13 08:51:16 14306