Using SQL Server 2008, I want to calculate the timespan, in seconds, that has occurred between two times.
The start date, is the timestamp of the last occurance of where a specific ID exists (if the filter is true), get the time from that timestamp record, and do a DATEDIFF() against the current processing time and return a value, @LastEventTimespan, in seconds.
DECLARE @CurrentProcessTime DATETIME
DECLARE @LastEventTimespan DATETIME
SET @CurrentProcessTime = GetDate()
-- find the timespan since the last session event
-- DATEDIFF ( datepart , startdate , enddate )
SELECT MAX(PageVisitEventID) AS LastPageVisitEventID, @LastEventTimespan = DATEDIFF(second , DateAdded , @CurrentProcessTime )
FROM PageVisitEvents
WHERE UserID = @UserID
GROUP BY LastPageVisitEventID
I figured I could get the MAX ID of the filter and process accordingly but am unable to set the @LastEventTimespan, however trying to assign a value when doing data-retrieval is a no-no.
How can I get around this?
Thanks.
I guess you want something like this.
This will calculate the difference in seconds between DateAdded for the highest value of PageVisitEventID for a given user and the current DateTime. I changed the data type of
@LastEventTimespantoINTbecause it probably makes more sense when dealing with seconds.