UPDATE:
If you need to do a PRODUCT() instead of a SUM(), there's obviously no built in aggregate for that. But, based on this answer, and a little row_number() magic, we can still achieve the desired result. I have a feeling there may be a smarter/faster way to do this, perhaps with Common Table Expressions, but I don't know what that would be.
select
the_date,
some_value,
(
select exp(sum(log(some_value)))
from (
select *, row_number() over (order by the_date, id) as row_num
from #tmp
) y
where y.row_num <= a.row_num
) as running_product
from
(
select *, row_number() over (order by the_date, id) as row_num
from #tmp t
) a
order by
the_date,
id
Previous answer:
The simplest way to do it is with a sum() over
like so:
if object_id('tempdb..#tmp') is not null drop table #tmp
create table #tmp (
id int identity(1,1) not null,
the_date datetime not null,
some_value decimal(12,2) not null
)
insert into #tmp (
the_date,
some_value
)
select '1/1/2013', rand() * 100 union all
select '2/1/2013', rand() * 100 union all
select '3/1/2013', rand() * 100 union all
select '4/1/2013', rand() * 100 union all
select '5/1/2013', rand() * 100 union all
select '6/1/2013', rand() * 100
select id, the_date, some_value,
sum(some_value) over (order by the_date, id) as total
from #tmp
And if you need to aggregate:
select
the_date,
some_value_sum,
sum(some_value_sum) over (order by the_date) total
from (
select the_date, sum(some_value) as some_value_sum
from #tmp
group by the_date
) x