I have a table with fields username and a lastUpdateDateTime. If the user makes more than one entry into this table I would like to see what the difference is between the first and last entry is in minutes and seconds. I thought I might use the function DATEDIFF but my database does not have a start date and end date as two separate fields. Should I use a cursor to loop the results ordered by username?
Not sure how to do this.
HERE IS THE LATEST…
SELECT username,
DATEDIFF(ss, MinDate, MaxDate) Assessment_Timespan,
AssessmentDate
FROM
(
SELECT storeID, username,
MIN(lastUpdateDateTime) MinDate,
MAX(lastUpdateDateTime) MaxDate,
CONVERT(VARCHAR(50), LastUpdateDateTime, 101) AS AssessmentDate
FROM Assessment
GROUP BY username, storeID, CONVERT(VARCHAR(50), LastUpdateDateTime, 101)
) src
You could use something like this. Which gets both the
minandmaxvalue for eachusername. Once you have that, then you can apply theDateDiff()function to it:See SQL Fiddle with Demo
This gives you the difference in seconds, you can then format it in any manner that you need.