100% догадок, основанных на очень смутных требованиях.
DECLARE @x TABLE
(
UserID INT,
FormID TINYINT,
StartTime DATETIME,
EndTime DATETIME
);
INSERT @x VALUES
-- a user who has completed all three forms
(1,1,'20110820 04:25:01','20110820 04:33:07'),
(1,2,'20110820 04:34:05','20110820 04:38:33'),
(1,3,'20110820 04:39:02','20110820 04:47:55'),
-- a user who has completed two forms
(2,1,'20110820 04:25:54','20110820 04:29:32'),
(2,2,'20110820 04:30:55','20110820 04:34:27'),
-- the same user who has completed 1.5 forms
(2,1,'20110820 04:35:23','20110820 04:37:15'),
(2,2,'20110820 04:38:34',NULL),
-- the same user who has completed all three forms
(2,1,'20110820 04:45:12','20110820 04:49:07'),
(2,2,'20110820 04:50:26','20110820 04:55:31'),
(2,3,'20110820 04:56:41','20110820 05:01:23'),
-- a slow user who has completed all three forms
(3,1,'20110820 05:25:04','20110820 05:43:07'),
(3,2,'20110820 05:44:09','20110820 05:55:21'),
(3,3,'20110820 05:59:41','20110820 06:24:23');
Вот некоторые типичные агрегаты, которые, как я предполагаю, могут попасть в ту, которую вы ищете:
SELECT -- avg by form regardless of user
FormID,
completed_forms = COUNT(*),
average = AVG(DATEDIFF(SECOND, StartTime, EndTime))
FROM @x GROUP BY FormID;
SELECT -- avg by form and user
UserID,
FormID,
completed_forms = COUNT(*),
[seconds] = AVG(DATEDIFF(SECOND, StartTime, EndTime))
FROM @x GROUP BY UserID, FormID;
SELECT -- avg by user regardless of form
UserID,
completed_forms = COUNT(*),
[seconds] = AVG(DATEDIFF(SECOND, StartTime, EndTime))
FROM @x GROUP BY UserID;
-- if you want hh:mm:ss format and the form never takes > 24 hours to complete,
-- you can do this kind of thing to any of the above queries:
;WITH x(FormID, completed_forms, average) AS
(
SELECT
FormID,
COUNT(*),
AVG(DATEDIFF(SECOND, StartTime, EndTime))
FROM @x GROUP BY FormID
)
SELECT
FormID,
completed_forms,
[hh:mm:ss] = CONVERT(CHAR(8), (CONVERT(TIME(0), DATEADD(SECOND, average, '19000101'))))
FROM x;