Oracle 语法之 OVER (PARTITION BY ..)
select * from test数据:
A B C
1 1 1
1 2 2
1 3 3
2 2 5
3 4 6
---将B栏位值相同的对应的C 栏位值加总
select a,b,c, SUM(C) OVER (PARTITION BY B) C_Sumfrom testA B C C_SUM
1 1 1 1
1 2 2 7
2 2 5 7
1 3 3 3
3 4 6 6
---如果不需要已某个栏位的值分割,那就要用 null
eg: 就是将C的栏位值summary 放在每行后面
select a,b,c, SUM(C) OVER (PARTITION BY null) C_Sumfrom testA B C C_SUM
1 1 1 17
1 2 2 17
1 3 3 17
2 2 5 17
3 4 6 17
this part from:
http://www.itpub.net/324056.html----------------
gan's practise:
SQL> select deptno,ename,job,sal from emp order by deptno; DEPTNO ENAME JOB SAL---------- ---------- --------- ---------- 10 CLARK MANAGER 2450 10 KING PRESIDENT 5000 10 MILLER CLERK 1300 20 JONES MANAGER 2975 20 FORD ANALYST 3000 20 ADAMS CLERK 1100 20 SMITH CLERK 800 20 SCOTT ANALYST 3000 30 WARD SALESMAN 1250 30 TURNER SALESMAN 1500 30 ALLEN SALESMAN 1600 30 JAMES CLERK 950 30 BLAKE MANAGER 2850 30 MARTIN SALESMAN 125014 rows selected.SQL> @select_over.sql DEPTNO ENAME JOB SAL SAL_PET---------- ---------- --------- ---------- ---------- 10 CLARK MANAGER 2450 28 10 KING PRESIDENT 5000 57.1428571 10 MILLER CLERK 1300 14.8571429 20 JONES MANAGER 2975 27.3563218 20 FORD ANALYST 3000 27.5862069 20 ADAMS CLERK 1100 10.1149425 20 SMITH CLERK 800 7.35632184 20 SCOTT ANALYST 3000 27.5862069 30 WARD SALESMAN 1250 13.2978723 30 TURNER SALESMAN 1500 15.9574468 30 ALLEN SALESMAN 1600 17.0212766 30 JAMES CLERK 950 10.106383 30 BLAKE MANAGER 2850 30.3191489 30 MARTIN SALESMAN 1250 13.297872314 rows selected.SQL> l 1 select deptno, ename, job, sal, 2 (sal*100)/sum(sal) over (partition by deptno) sal_pet 3* from emp
阅读(1846) | 评论(0) | 转发(0) |