| Author |
Topic |
|
zion99
Posting Yak Master
141 Posts |
Posted - 2009-07-29 : 05:09:20
|
| Hi All,I need to calculate average values for a particular periodFormula is as follows:if the average duration is selected as 4 days average value for a particular day will be(current days value + last 3 days' values) / 4===========if the average duration is selected as 7 days average value for a particular day will be(current days value + last 6 days' values) / 7i can use a cursor to calculate each value, but is there a better way to do this?[CODE]declare @tmptbl table (id int,dateval datetime,val float,avgval float)insert into @tmptbl(id,dateval,val)select 1,'2009-01-27 00:00:00.000', 32 UNION ALLselect 2,'2009-01-28 00:00:00.000', 45 UNION ALLselect 3,'2009-01-29 00:00:00.000', 92 UNION ALLselect 4,'2009-01-30 00:00:00.000', 34 UNION ALLselect 5,'2009-03-15 00:00:00.000', 96 UNION ALLselect 6,'2009-03-16 00:00:00.000', 87 UNION ALLselect 7,'2009-03-17 00:00:00.000', 13 UNION ALLselect 8,'2009-05-01 00:00:00.000', 30select * from @tmptbl[/CODE] |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-29 : 05:14:44
|
| How do you determine current day's value?MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-29 : 06:18:32
|
the "average duration selected" is a parameter pass in to your query ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
zion99
Posting Yak Master
141 Posts |
Posted - 2009-07-29 : 06:23:34
|
| "average duration selected" is a parameter passed to the query |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-29 : 06:30:34
|
like this ?DECLARE @tmptbl TABLE ( id int ,dateval datetime ,val float ,avgval float)INSERT INTO @tmptbl(id,dateval,val)SELECT 1,'2009-01-27 00:00:00.000', 32 UNION ALLSELECT 2,'2009-01-28 00:00:00.000', 45 UNION ALLSELECT 3,'2009-01-29 00:00:00.000', 92 UNION ALLSELECT 4,'2009-01-30 00:00:00.000', 34 UNION ALLSELECT 5,'2009-03-15 00:00:00.000', 96 UNION ALLSELECT 6,'2009-03-16 00:00:00.000', 87 UNION ALLSELECT 7,'2009-03-17 00:00:00.000', 13 UNION ALLSELECT 8,'2009-05-01 00:00:00.000', 30DECLARE @today datetime, @days intSELECT @today = '2009-01-30', @days = 3SELECT AVG(val)FROM @tmptblWHERE dateval >= DATEADD(DAY, -@days + 1, @today)AND dateval <= @today KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
zion99
Posting Yak Master
141 Posts |
Posted - 2009-07-29 : 06:30:43
|
| its like... every date will be taken as current date - formula will be applied to calculate the average value for that particular date. |
 |
|
|
zion99
Posting Yak Master
141 Posts |
Posted - 2009-07-29 : 07:19:23
|
| hi khtan, even in this scenario, i will have to use cursor which i wanted to avoid in the first place.I need to calculate avgval value for all the available records (8 for this table) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-29 : 07:34:22
|
[code]DECLARE @tmptbl TABLE ( id int ,dateval datetime ,val float ,avgval float)INSERT INTO @tmptbl(id,dateval,val)SELECT 1,'2009-01-27 00:00:00.000', 32 UNION ALLSELECT 2,'2009-01-28 00:00:00.000', 45 UNION ALLSELECT 3,'2009-01-29 00:00:00.000', 92 UNION ALLSELECT 4,'2009-01-30 00:00:00.000', 34 UNION ALLSELECT 5,'2009-03-15 00:00:00.000', 96 UNION ALLSELECT 6,'2009-03-16 00:00:00.000', 87 UNION ALLSELECT 7,'2009-03-17 00:00:00.000', 13 UNION ALLSELECT 8,'2009-05-01 00:00:00.000', 30DECLARE @days intSELECT @days = 4SELECT id, dateval, val, a.avgvalFROM @tmptbl t CROSS APPLY ( SELECT avgval = avg(val) FROM @tmptbl x WHERE x.dateval >= DATEADD(DAY, -@days + 1, t.dateval) AND x.dateval <= t.dateval ) a/*id dateval val avgval ----------- ----------- ------- --------------------1 2009-01-27 32.0 32.02 2009-01-28 45.0 38.53 2009-01-29 92.0 56.3333333333333364 2009-01-30 34.0 50.755 2009-03-15 96.0 96.06 2009-03-16 87.0 91.57 2009-03-17 13.0 65.3333333333333298 2009-05-01 30.0 30.0(8 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|