Tim Burns' Personal Weblog Flavored with Math, Java, and Oracle
 
Thursday, April 22, 2004 [*]
 

Calculating a Moving Average In Oracle

If for no other reason than to delude myself into thinking my blogging is worthwhile, I hope to learn something important enough to blog at least once a week. Today I learned the power of the OVER command in Oracle SQL and how to apply it to get a moving average of data.

I picked a small dataset that reflects my regrets that I didn't pick a career path of collecting samples from rivers and doing data analysis. I have to content myself with going to meetings and doing data analysis but that is another story.

        SELECT TO_DATE(pil.sample_date, 'YYYYMMDD') AS sample_date, 
    	   pil.nh4,
    	   TRUNC(AVG(pil.nh4)
    	   	 OVER (ORDER BY pil.sample_date 
                           ROWS BETWEEN 1 PRECEDING 
    		       AND 1 FOLLOWING), 6) AS avg_nh4
        FROM plum_island_lter pil 
        WHERE permanent_id='YSI-CC'
    

The SQL statement will work well for very large datasets and fills in null data with the average. Here is a graph of the moving average. Notice that the breaks in the data (the blue line) are filled in nicely by the rolling averages.

 
Comments [0]
 
xml

Personal Favorites

Southwestern Baby Shower Menu

Morning of the Wolf

Autumn lingers in New England

Foraging and Shopping

 

Professional Favorites

Calculating a Moving Average In Oracle
The SVD, Benchmarking and Optimization
The Problem with Software Engineers
Fun with Optimization
Anything new under the sun?
Really Simple Tomcat, Really
J2EE with Free Software
 

The Daily Chronic

Brown News
Carl Zimmer
Dijkstra
Dive into Mark
Get Fuzzy
Instapundit
Kevin Drum
Late Night Hacking
Lorrianne Schaub
Mark Rittman
Mike Shoemaker
Philip Greenspun
Photo Matt
Salon
Science Blog
Signal+Noise
Steve Button
Woods Hole News
 

References

BioInformatics

BioTech

Epicurious

Statistics

 

Archive

2004
 April 01 04 07 09 11 16 17 18 20 22 24 25
 March 02 15 21 22 24 27
 February 13
 January 04 16 19 29
2003
 December 06 12
 November 02 23 29
 October 07 11 12 16 18 21 24 26
 September 04 09 10 12 14 28
 August 05 08 13 17 18 25 29
 July
 June
2002
2001
 

Photos

Morning of the Wolf
Phantom Farms
Providence
Baby Shower
Camden, Maine
 

Wiki

Creative Commons License
This work is licensed under a Creative Commons License