分析函數中有ORDER BY的存在將添加一個默認的開窗子句!意味著從分區的第一行到當前行;
分析函數中沒有ORDER BY時,默認的窗口是分區內的全部 ;
在Order by 子句後可以添加nulls last,如:order by comm desc nulls last 表示排序時忽略comm列為空的行.
不寫between AND ,在有order BY 的情況下,就是分組第一行到當前行 BETWEEN unbounded preceding and current row
不寫between AND ,在沒有order BY 的情況下,就是分組第一行到分組最後一行; BETWEEN unbounded preceding and unbounded following
此外記住,在RANGE的開窗中,ORDER BY中只能有一列;ROWS的開窗的ORDER BY 可以有多列。
下面是一個例子:
SELECT emp_id,ename,dept_id,hire_date,sal, SUM(sal) OVER (PARTITION BY dept_id ORDER BY hire_date) sum_sal1, SUM(sal) OVER (PARTITION BY dept_id ORDER BY hire_date DESC) sum_sal2, SUM(sal) OVER (PARTITION BY dept_id ORDER BY hire_date DESC nulls LAST) sum_sal3, SUM(sal) OVER (PARTITION BY dept_id ) sum_sal4, SUM(sal) OVER ( ) sum_sal5 FROM emp; EMP_ID ENAME DEPT_ID HIRE_DATE SAL SUM_SAL1 SUM_SAL2 SUM_SAL3 SUM_SAL4 SUM_SAL5 ------ ----- ------- -------------- ---------- ---------- ---------- ---------- ---------- ---------- 100 Stev 10 01-1月 -90 7000 7000 7000 7000 7000 36000 101 Tom 20 21-9月 -89 2000 2000 10000 10000 10000 36000 102 Mike 20 13-1月 -93 8000 10000 8000 8000 10000 36000 120 John 50 18-7月 -96 1000 1000 19000 16000 19000 36000 121 Joy 50 10-4月 -97 4000 5000 18000 15000 19000 36000 123 Kate 50 10-10月-97 5000 10000 14000 11000 19000 36000 124 Jess 50 16-11月-99 6000 16000 9000 6000 19000 36000 122 Rich 50 3000 19000 3000 19000 19000 36000