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 |
|
inertia
Starting Member
3 Posts |
Posted - 2007-06-13 : 12:44:00
|
| I've found a few statements that almost get what I need, but I'm having a little difficulty tweaking them to meet my needs.Basic Table: TransDate DateTimeAmount DecimalWhat I'd like to do is get a cumlative total by week.any help is greatly appreciated... |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-13 : 13:00:16
|
| Can you post some sample data and the expected out put so we can understand the question better?Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
inertia
Starting Member
3 Posts |
Posted - 2007-06-13 : 15:26:41
|
| Sample data would like this:datetime Amount6/4/2007 01:12:00 100.006/4/2007 02:33:00 50.006/5/2007 13:40:00 80.006/11/2007 01:12:00 100.006/12/2007 13:10:0 100.00results should look something like:Week CumlativeAmount23 230.0024 430.00(I suppose if I were to get fancy I'd also add a weekly sum column)thanks |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-13 : 15:48:35
|
| select datepart(week,yourdate) as [Week], sum(Amount) as Totalfrom yourtablegroup by datepart(week,yourdate)- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
inertia
Starting Member
3 Posts |
Posted - 2007-06-13 : 15:57:20
|
| Jeff,close, but that provides a sum for each week.I'm looking for a cumlative total by week (see above expected results)thanks for the help... |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-14 : 00:11:35
|
| Create table tt(dt datetime, Amount decimal)insert ttselect '6/4/2007 01:12:00', 100.00 union allselect '6/4/2007 02:33:00', 50.00 union allselect '6/5/2007 13:40:00', 80.00 union allselect '6/11/2007 01:12:00', 100.00 union allselect '6/12/2007 13:10:0', 100.00 union allselect '7/12/2007 13:10:0', 100.00 union allselect '7/13/2007 13:10:0', 100.00GoCreate function fn_cumsum(@wk int)returns decimalasbegin declare @sum decimal Select @sum = Coalesce(@sum, 0) + sum(amount) from tt where datepart(ww, dt) <= @wk Return @sumEndSelect datepart(ww, dt) as [Week], Sum(amount) as [Sum], dbo.fn_cumsum(datepart(ww, max(dt))) as [Cumulative Sum] from tt group by datepart(ww, dt)--------------------------------------------------S.Ahamed |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-14 : 09:15:52
|
| Where do you want to show data?If you use Reporting tool make use of its Running Total featureMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|