| Author |
Topic |
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-12 : 19:43:44
|
| I have daily data that I would like to sum a running total and group by week.1/1/2001, 51/2/2001, 51/3/2001, 51/4/2001, 51/5/2001, 51/6/2001, 51/7/2001, 51/8/2001, 51/9/2001, 51/10/2001, 5So my results would look like1, 352, 50I've tried this but it throws an error...select datename(mm,c.TimeByDay) , (select sum(b.data) from myTable b where b.timebyDay <= c.timebyDay) as running_total_by_weekfrom myTable c group by datename(mm,c.TimeByDay) |
|
|
ftsoft
Starting Member
22 Posts |
Posted - 2009-11-12 : 20:27:30
|
| I use this code to sum bicycle ride totals by week.SELECT SUM(distance) As weeklydistancetotal, SUM(HOURS) + SUM(MINUTES)/60 AS weeklyhourstotal FROM rideevent /*WHERE DATENAME(yyyy,RIDEDATE) = DATENAME(yyyy,@date) AND DATENAME(week,RIDEDATE) = DATENAME(week,@date)*/ where DATEPART(WEEK,RIDEDATE) = datepart(week, @date) AND DATEPART(YEAR, RIDEDATE) = DATEPART(YEAR, @date)Hope this helps. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-12 : 20:44:06
|
Note : datename(mm,c.TimeByDay) will give you January, February etc not week noWhat is your definition of week ?declare @sample table( [TimeByDay] datetime, [data] int)insert into @sampleselect '1/1/2001', 5 union allselect '1/2/2001', 5 union allselect '1/3/2001', 5 union allselect '1/4/2001', 5 union allselect '1/5/2001', 5 union allselect '1/6/2001', 5 union allselect '1/7/2001', 5 union allselect '1/8/2001', 5 union allselect '1/9/2001', 5 union allselect '1/10/2001', 5-- Using datepart(week, ) definition of week noselect WeekNo = datepart(week, [TimeByDay]), RuningTotal = (select sum(data) from @sample x where datepart(week, x.[TimeByDay]) <= datepart(week, s.[TimeByDay]))from @sample sgroup by datepart(week, [TimeByDay])/*WeekNo RuningTotal ----------- ----------- 1 302 50*/select s.WeekNo, r.RunningTotalfrom ( select distinct WeekNo = datepart(week, [TimeByDay]) from @sample ) s cross apply ( select RunningTotal = sum(data) from @sample x where datepart(week, x.[TimeByDay]) <= s.WeekNo ) r/*WeekNo RuningTotal ----------- ----------- 1 302 50*/-- Using 1st of the year as begining of the weekselect WeekNo = (datepart(dayofyear, TimeByDay) - 1) / 7 + 1, RuningTotal = (select sum(data) from @sample x where (datepart(dayofyear, x.TimeByDay) - 1) / 7 + 1 <= (datepart(dayofyear, s.TimeByDay) - 1) / 7 + 1)from @sample sgroup by (datepart(dayofyear, TimeByDay) - 1) / 7 + 1/*WeekNo RuningTotal ----------- ----------- 1 352 50*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-12 : 21:24:16
|
| thanks...this looks great!! yep my mistake on the mm rather than ww. |
 |
|
|
matrixmind
Starting Member
9 Posts |
Posted - 2009-11-13 : 01:34:15
|
quote: Originally posted by JohnBGood I have daily data that I would like to sum a running total and group by week.1/1/2001, 51/2/2001, 51/3/2001, 51/4/2001, 51/5/2001, 51/6/2001, 51/7/2001, 51/8/2001, 51/9/2001, 51/10/2001, 5So my results would look like1, 352, 50I've tried this but it throws an error...select datename(mm,c.TimeByDay) , (select sum(b.data) from myTable b where b.timebyDay <= c.timebyDay) as running_total_by_weekfrom myTable c group by datename(mm,c.TimeByDay)
with abcas(select Datepart(week,IDate) as [W],Sum(value) as ,Row_Number() over(Order by Datepart(week,IDate)) as [R] From RunnGroup by Datepart(week,IDate))select W,(select Sum(V) From Abc A Where A.R<=Abc.R) as From AbcDinesh SharmaMatrix SolutionSr.Software Engg. |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-13 : 08:00:32
|
| These are great solutions!! what can i do if my data spans years?? Otherwise I get1, 52, 103, 1550, 2051, 2552, 30 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-13 : 08:26:48
|
yes. Group by yearselect s.Year, s.WeekNo, r.RunningTotalfrom ( select distinct Year = datepart(year, [TimeByDay]), WeekNo = datepart(week, [TimeByDay]) from @sample ) s cross apply ( select RunningTotal = sum(data) from @sample x where datepart(year, x.[TimeByDay]) <= s.Year and datepart(week, x.[TimeByDay]) <= s.WeekNo ) rselect Year = datepart(week, [TimeByDay]), WeekNo = (datepart(dayofyear, TimeByDay) - 1) / 7 + 1, RuningTotal = (select sum(data) from @sample x where (datepart(dayofyear, x.TimeByDay) - 1) / 7 + 1 <= (datepart(dayofyear, s.TimeByDay) - 1) / 7 + 1)from @sample sgroup by datepart(week, [TimeByDay]), (datepart(dayofyear, TimeByDay) - 1) / 7 + 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-13 : 09:30:38
|
| great ... i will give this a try! Thanks so much. |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-13 : 12:52:23
|
| --I really appreciate the help.. I've tried each of these and they don't quite do the trickdeclare @sample table( [TimeByDay] datetime, [data] int)insert into @sampleselect '12/26/2001', 5 union allselect '12/27/2001', 5 union allselect '12/28/2001', 5 union allselect '12/29/2001', 5 union allselect '12/30/2001', 5 union allselect '12/31/2001', 5 union allselect '1/1/2002', 5 union allselect '1/2/2002', 5 union allselect '1/3/2002', 5 union allselect '1/4/2002', 5 union allselect '1/5/2002', 5 union allselect '1/6/2002', 5 union allselect '1/7/2002', 5 union allselect '1/8/2002', 5 union allselect '1/9/2002', 5 union allselect '1/10/2002', 5 union allselect '1/11/2002', 5 select s.Year, s.WeekNo, r.RunningTotalfrom ( select distinct Year = datepart(year, [TimeByDay]), WeekNo = datepart(week, [TimeByDay]) from @sample ) s cross apply ( select RunningTotal = sum(data) from @sample x where datepart(year, x.[TimeByDay]) <= s.Year and datepart(week, x.[TimeByDay]) <= s.WeekNo ) r-- the result I need is -- the running total spans over the year change2001, 52, 202001, 53, 302002, 1, 552002, 2, 85 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-13 : 18:49:22
|
[code]select s.Year, s.WeekNo, r.RunningTotalfrom ( select distinct Year = datepart(year, [TimeByDay]), WeekNo = datepart(week, [TimeByDay]), YearWeek = datepart(year, [TimeByDay]) * 1000 + datepart(week, [TimeByDay]) from @sample) scross apply( select RunningTotal = sum(data) from @sample x where datepart(year, x.[TimeByDay]) * 1000 + datepart(week, x.[TimeByDay]) <= s.YearWeek) r[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-11-14 : 07:45:45
|
| That works well khtan!! thanks so very much for your excellent solution! I would have still been banging my head if it were not for your help. Gonna have to look up the cross apply operation - as i've never come across it. |
 |
|
|
|
|
|