Sunday, May 2, 2010

Here I needed to get the sum of values for intervals of every 5 minutes.First thing that came to my mind was to use the evil "Cursors" to achieve it.But somehow I found a different way of doing it.Here is the sample data

The o/p should be 7,13 & 3 as slot for which I needed to take the sum of values was every 5 minutes.Here is the query.

declare @tbl as table(dt datetime,value int)
insert into @tbl
select '03-03-2009 10:15:10',5 union all
select '03-03-2009 10:17:10',2 union all
select '03-03-2009 10:22:25',3 union all
select '03-03-2009 10:24:00',10 union all
select '03-03-2009 10:29:00' ,3

select * from @tbl

declare @slot int=5
select SUM(value)as SumOfSlots from @tbl group by DATEPART(minute,dt)/@slot*@slot

You can set the value of the @slot variable to value in minutes for which you want to have the Sum.

No comments:

Post a Comment