Function :Moving
Average
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Teradata Syntax:
Following is the Teradata way of performing moving average.
In
the below example we have moving average window as 7
SELECT
EMPLOYEEID,DEPARTMENTNO,HIREDATE,SALARY,
MAVG(SALARY,7,HIREDATE)
FROM
EMPLOYEE;
In
the above result we can see that the value of moving avg is equal to the avg
of the salary in the current row and 6 rows above it.
When there are less than 6 rows above the current rows, average is calculated based upon the existing rows.
In
the above
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ANSI syntax:
Following is the ANSI syntax of getting similar result as above
SELECT
EMPLOYEEID,DEPARTMENTNO,HIREDATE,SALARY,
AVG(SALARY) OVER (ORDER BY HIREDATE ROWS 6 PRECEDING)
FROM
EMPLOYEE;
In
the above syntax:
Note that this number indicates the number of rows above the
current row.
Thus in the above example to have a window of 7 , we need to
use 'ROWS 6 PRECEDING'.
However when working with MAVG we need to write the 7 to
achieve a window width of 7.
|
Pages
▼
No comments:
Post a Comment