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.
| 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] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|