Calculates an exponential weighted moving average.
Similar to the WEIGHTED_AVG function, the weights in EXP_WEIGHTED_AVG decrease over time. However, weights in WEIGHTED_AVG decrease arithmetically, whereas weights in EXP_WEIGHTED_AVG decrease exponentially. Exponential weighting applies more weight to the most recent values, and decreases the weight for older values while still applying some weight.
Sybase IQ calculates the exponential moving average using:
S*C+(1-S)*PEMA
In the calculation above, Sybase IQ applies the smoothing factor by multiplying the current closing price (C) by the smoothing constant (S) added to the product of the previous day’s exponential moving average value (PEMA) and 1 minus the smoothing factor.
Sybase IQ calculates the exponential moving average over the entire period specified by the OVER clause. period-expression specifies the moving range of the exponential moving average.
You can specify elements of window-spec either in the function syntax (inline), or with a WINDOW clause in the SELECT statement. The window-spec must contain an ORDER BY statement and cannot contain a frame specification.
SELECT DepartmentID, Surname, Salary,EXP_WEIGHTED_AVG(Salary, 3) OVER (ORDER BY YEAR(StartDate) DESC) as "W_AVG"FROM EmployeesWHERE State IN ('FL') ORDER BY StartDate DESC
The returned result set is:
DepartmentID |
Surname |
Salary |
W_AVG |
---|---|---|---|
400 |
Evans |
68,940.000 |
34,470.000000 |
300 |
Litton |
58,930.000 |
46,700.000000 |
200 |
Sterling |
64,900.000 |
55,800.000000 |
200 |
Kelly |
87,500.000 |
71,650.000000 |
400 |
Charlton |
28,300.000 |
49,975.000000 |
100 |
Lull |
87,900.000 |
68,937.500000 |
100 |
Gowda |
59,840.000 |
60,621.875000 |
400 |
Francis |
53,870.000 |
61,403.750000 |