oracle's analytic function over
create table TEST001
(
EMPNO NUMBER(4),
ENAME VARCHAR2(50),
SAL NUMBER(8,4)
)
INSERT INTO TEST001 values(3,'James',1000);
INSERT INTO TEST001 values(3,'Allen',800);
INSERT INTO TEST001 values(2,'Blake',600);
INSERT INTO TEST001 values(2,'Ward',300);
INSERT INTO TEST001 values(1,'Turner',1200);
1. Total amount of each department
select empno,sum(sal) as department total amount from test001 group by empno;
2. The proportion of the total amount of each department
select empno, empno_total,total,round(empno_total/total*100,2)||'%' as pst
from( select empno, empno_total, sum(empno_total) over() as total
from (select empno,sum(sal) empno_total from test001 group by empno))
3. Individuals account for the total
select empno,ename,sal,round(sal/total,2) pst from
(select empno,ename,sal,sum(sal) over() as total from test001 order by 1)
4.依次累加sum() over(order by)
select empno,ename,sal,sum(sal) over(order by empno) as total from test001 order by 1
5.分区累加sum() over(partition by)
select empno,ename,sal,sum(sal) over(partition by empno ) as total from test001 order by 1
Similar to select empno,sum(sal) from test001 group by empno, but cannot get the value outside the grouping field
6. Accumulate sum(xx) over(partition by xx order by xx) in the area
select empno,ename,sal,sum(sal) over(partition by empno order by ename ) as total from test001 order by 1