|
|
Re: ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING [message #430778 is a reply to message #430769] |
Thu, 12 November 2009 05:38   |
ayush_anand
Messages: 417 Registered: November 2008
|
Senior Member |
|
|
Analytic Example
The following example calculates, for each employee in the employees table, the average salary of the employees reporting to the same manager who were hired in the range just before through just after the employee:
SELECT manager_id, last_name, hire_date, salary,
AVG(salary) OVER (PARTITION BY manager_id ORDER BY hire_date
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS c_mavg
FROM employees;
MANAGER_ID LAST_NAME HIRE_DATE SALARY C_MAVG
---------- ------------------------- --------- ---------- ----------
100 Kochhar 21-SEP-89 17000 17000
100 De Haan 13-JAN-93 17000 15000
100 Raphaely 07-DEC-94 11000 11966.6667
100 Kaufling 01-MAY-95 7900 10633.3333
100 Hartstein 17-FEB-96 13000 9633.33333
100 Weiss 18-JUL-96 8000 11666.6667
100 Russell 01-OCT-96 14000 11833.3333
http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/functions010.htm#SQLRF00609
SQL> SELECT x, y,
2 AVG(y) OVER(ORDER BY x
3 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) ma
4 FROM TestTable
5 ORDER BY x;
X Y MA
---------- ---------- ----------
1 7 4
2 1 3.33333333
3 2 2.66666667
4 5 4.66666667
5 7 15.3333333
6 34 24.3333333
7 32 36.3333333
8 43 54
9 87 54
10 32 43.6666667
11 12 20
12 16 30.3333333
13 63 51
14 74 57.6666667
15 36 55.3333333
16 56 31.3333333
17 2 29
17 rows selected.
In the example at the link if you look at X=1
then MA=(1+7)/2 = 4
similarily for X=2
MA=(7+1+2)/3=10/3=3.33333
so it means
Michel saidThis means Oracle take the row preceding the current, the current row and the row following the current row and apply the function.
[Updated on: Thu, 12 November 2009 05:44] Report message to a moderator
|
|
|
|
|