Calculating moving average

From: <nickli2000_at_gmail.com>
Date: Sat, 17 May 2008 23:19:00 -0700 (PDT)
Message-ID: <fbeeef55-85d1-4b69-b80b-09c4aa28866a@8g2000hse.googlegroups.com>


Hi,

  I am trying to calculating the following with SQL:

     Stock    Date        Price
      X        5/1/2008    10
      X        5/2/2008    12
      X        5/3/2008    13
      Y        5/1/2008    20
      Y        5/2/2008    22
      Y        5/3/2008    23
      ..........

      It will be a month of data with stock, date, price as unique,
with thousands of symbols.

      Today's moving average is calculated based on previous days' moving average plus today's price. The sum will be divided by the number of days as in the following:

      For stock X:
      The moving average on 5/1/2008 will be 10
      The moving average on 5/2/2008 will be (10 + 12) / 2 = 11
      The moving average on 5/2/2008 will be (10 + 11 + 13) / 3 =
11.33
      The same for the stock Y:
      The moving average on 5/1/2008 will be 20
      The moving average on 5/2/2008 will be (20 + 22) / 2 = 21
      The moving average on 5/2/2008 will be (20 + 21 + 23) / 3 =
21.33

      I am trying to using windowing function to get the moving aveage but it doesn't seem to be working. Do I have to use PL/SQL to accomplish this?

      Thanks in advance.

      Nick Li Received on Sun May 18 2008 - 01:19:00 CDT

Original text of this message