タイムスパンに使用できDATEADD
ますGROUP BY
:
Select Count(*), DateAdd(Minute, @Interval * (DateDiff(Minute, 0, SomeDate) / @Interval), 0)As Part
From #Temp
Where SomeDate Between @StartTime And @EndTime
Group By DateAdd(Minute, @Interval * (DateDiff(Minute, 0, SomeDate) / @Interval), 0)
ORDER BY Part
サンプルデータ:
declare @StartTime datetime;
declare @EndTime datetime;
declare @Interval int;
SET @StartTime = Convert(datetime,'2012-10-19 12:00:00',102);
SET @EndTime = Convert(datetime,'2012-10-19 13:00:00',102);
SET @Interval = 20;
create table #Temp(SomeDate datetime);
insert into #Temp values(Convert(datetime,'2012-10-19 12:05:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:06:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:15:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:25:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:45:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:35:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:37:20',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:15:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:55:00',102));
insert into #Temp values(Convert(datetime,'2012-10-19 12:18:10',102));
ここにフィドルがあります:http://sqlfiddle.com/#!3/ee6f9/1/0