Following script should get you started.
A few notes of warning though
- it does not work in its current form if starttime and endtime are not the same day
- there are no checks in place wether starttime is less than endtime
SQL Script
DECLARE @People TABLE (
Name VARCHAR(32)
, StartTime DATETIME
, EndTime DATETIME
)
DECLARE @MinHour INTEGER
DECLARE @MaxHour INTEGER
DECLARE @Times TABLE (
Hour INTEGER
, Minute INTEGER
)
INSERT INTO @People
SELECT 'richard', '2010-04-21 08:01:15', '2010-04-21 08:06:15'
UNION ALL SELECT 'bill' , '2010-04-21 08:07:45', '2010-04-21 08:11:15'
SELECT @MinHour = MIN(DATEPART(hh, StartTime))
, @MaxHour = MAX(DATEPART(hh, EndTime))
FROM @People
WHILE @MinHour < @MaxHour + 1
BEGIN
INSERT INTO @Times
SELECT @MinHour, 0
UNION ALL SELECT @MinHour, 5
UNION ALL SELECT @MinHour, 10
UNION ALL SELECT @MinHour, 15
UNION ALL SELECT @MinHour, 20
UNION ALL SELECT @MinHour, 25
UNION ALL SELECT @MinHour, 30
UNION ALL SELECT @MinHour, 35
UNION ALL SELECT @MinHour, 40
UNION ALL SELECT @MinHour, 45
UNION ALL SELECT @MinHour, 50
UNION ALL SELECT @MinHour, 55
SET @MinHour = @MinHour + 1
END
SELECT p.Name
, t.Hour
, t.Minute
, CASE WHEN DATEPART(mi, p.EndTime) - DATEPART(mi, p.EndTime) % 5 = t.Minute
THEN 60 * (DATEPART(mi, p.EndTime) % 5) + DATEPART(ss, p.EndTime)
ELSE 300 - 60 * (DATEPART(mi, p.StartTime) % 5) - DATEPART(ss, p.StartTime)
END
FROM @People p
INNER JOIN @Times t ON DATEPART(hh, p.StartTime) = t.Hour
AND DATEPART(hh, p.EndTime) = t.Hour
AND DATEPART(mi, p.StartTime) - DATEPART(mi, p.StartTime) % 5 <= t.Minute
AND DATEPART(mi, p.EndTime) - DATEPART(mi, p.EndTime) % 5 >= t.Minute
ORDER BY
p.Name