DB

Oracle 분석 함수 주의점

Lawmin 2012. 7. 17. 16:09

1. Window를 지정하지 않으면 데이터가 틀어진다. 아래 SQL을 통해 확인

select  ename, deptno, sal,

        sum(sal) over (partition by deptno)  "부서별급여합계",

        sum(sal) over (partition by deptno order by sal) "부서별 급여오름차순누적",

        sum(sal) over (partition by deptno order by sal rows unbounded preceding) "부서별 급여오름차순누적",

        sum(sal) over (order by deptno, sal) "부서별 급여오름차순 누적",

        sum(sal) over (order by deptno, sal rows unbounded preceding) "부서별 급여오름차순 누적",

        sum(sal) over()  "전체급여"

from    emp

order by deptno; 

MILLER 10 2600 17500 2600 2600 2600 2600 37775
CLARK 10 4900 17500 7500 7500 7500 7500 37775
KING 10 10000 17500 17500 17500 17500 17500 37775
SMITH 20 800 10875 800 800 18300 18300 37775
ADAMS 20 1100 10875 1900 1900 19400 19400 37775
JONES 20 2975 10875 4875 4875 22375 22375 37775
FORD 20 3000 10875 10875 7875 28375 25375 37775
SCOTT 20 3000 10875 10875 10875 28375 28375 37775
JAMES 30 950 9400 950 950 29325 29325 37775
MARTIN 30 1250 9400 3450 2200 31825 30575 37775
WARD 30 1250 9400 3450 3450 31825 31825 37775
TURNER 30 1500 9400 4950 4950 33325 33325 37775
ALLEN 30 1600 9400 6550 6550 34925 34925 37775
BLAKE 30 2850 9400 9400 9400 37775 37775 37775