Hi
I've got records that indicate when a job starts and when it ends. The End time is not recorded until the job ends, but the start time exists from the time the job starts. What I want is to know how many jobs were running in a given time period.
declare @data table (
JobId INT IDENTITY(1,1),
StartedAt DATETIME NOT NULL,
EndedAt DATETIME NULL
)
insert into @data (StartedAt, EndedAt)
select '1 Jan 2010 8:00', '1 Jan 2010 8:30'
union select '1 Jan 2010 8:00', '1 Jan 2010 9:00'
union select '1 Jan 2010 8:00', '1 Jan 2010 9:20'
union select '1 Jan 2010 9:00', '1 Jan 2010 9:20'
union select '1 Jan 2010 9:10', NULL
Given the above, how would I query the number of jobs running in each hour? I would expect the results to indicate that there were 3 jobs running in the 8:00 through 8:59 time period, and 4 running in the 9:00 through 9:59 period, as follows:
Time period Jobs 08:00..08:59 3 09:00..09:59 4
I can modify the schema and I have some influence over how data is recorded if that would make the query simpler or more efficient.Blockquote