database - SQL calculations: Perform AVERAGE using THIS year, LAST year and NEXT year data? -
i’m struggling recreate calculation in sql can in excel. concerns averages.
we collect data on many years. 1 table create 3 year average. below tables show: 1) raw data, , 2) new table average data. ‘dimensions’ [year] & [itemcode].
the average data based on averaging year’s figure 2 years either side. i.e. 1991 average figure = average (1990,1991,1992). =average(b2:d2)
1) raw alt text http://i55.tinypic.com/o9pc8g.jpg
2) average
alt text http://i56.tinypic.com/5d12dz.jpg
in excel seems simple do: average formula dragged other cells, or in vba using r1c1 referencing.
but in sql? how use ‘this’ year & year before & year after data?
also, realise if there’s empty cell throw calculations, how manage null values calculates when there 3 figures use?...and again until finished (many years , many, many itemcodes)
any appreciated.
you don't state technology, assuming mssql.
assume mydate 2010
select dateadd(year, -1, mydate), mydate, dateadd(year, 1, mydate) somethingorother
returns
2009, 2010, 2011
assume x, y, z null
select coalesce(x, y, z, 0)
returns
0
Comments
Post a Comment