This is my attempt, it works on a 24 hour widow and im sure theres a better way of doing it
CREATE TABLE #Hist
(
CallDateTime DateTime
,HistId int
)
Insert into #Hist
VALUES ('2013-02-01 06:14:41.000',2671058)
,('2013-02-01 07:02:15.000', 2671057)
,('2013-02-01 07:06:41.000', 2671059)
,('2013-02-01 07:07:20.000', 2671069)
,('2013-02-01 07:22:57.000', 2671068)
,('2013-02-01 07:38:15.000', 2671067)
,('2013-02-01 07:42:48.000', 2671066)
,('2013-02-01 07:53:47.000', 2671065)
,('2013-02-01 08:02:37.000', 2671064)
,('2013-02-01 08:07:04.000', 2671063)
,('2013-02-01 08:11:09.000', 2671062)
,('2013-02-01 08:22:12.000', 2671061)
,('2013-02-01 08:22:48.000', 2671060)
Declare @StartDate Datetime = '01-Feb-2013 00:00:00.000'
Declare @endDate Datetime = '02-Feb-2013 00:00:00.000'
;with CTE
AS
(
Select
Row_NUMBER() OVER (ORDER BY object_id) Rn
from sys.all_columns
)
,Cte_Interval AS
(
Select
DateAdd(minute,(rn-1)*30,@StartDate) StartTime
, DateAdd(minute,(rn)*30,@StartDate) EndTime
from CTE
Where DateAdd(minute,(rn)*30,@StartDate)<=@EndDate
)
Select StartTime,EndTime,count(histId)
from Cte_Interval inter
LEFT JOIN #Hist hist on hist.CallDateTime >=inter.StartTime
and hist.CallDateTime<inter.EndTime
group by StartTime,EndTime
order by StartTime
_________________________________________________________________________
SSC Guide to Posting and Best Practices