Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 running total by month

Author  Topic 

Movember
Starting Member

12 Posts

Posted - 2009-11-25 : 17:39:08
Using sql2008 trying to make a running total field that accumulates per week. This is the query i am using to split into months/weeks. Not sure how to do the running total.

SELECT title,
YEAR(paymentDate) AS 'Year',
datename(mm, paymentDate) AS 'Month',
datepart(wk,paymentDate) AS 'Week',
count(*) AS [Units sold],
count(title) AS [Running total]
from transactions

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-11-25 : 19:22:55
[code]
SELECT title,
YEAR(paymentDate) AS 'Year',
datename(mm, paymentDate) AS 'Month',
datepart(wk,paymentDate) AS 'Week',
count(*) AS [Units sold],
r.[Running total]
from transactions t
cross apply
(
select count(*) AS [Running total]
from transactions x
where x.title = t.title
and datediff(wk, 0, x.paymentDate) <= datediff(wk, 0, t.paymentDate)
) r
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-26 : 01:56:35
Also play with running total method
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -