Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 실습
- seaborn
- sklearn
- Python
- Kaggle
- 알고리즘
- pandas
- Oracel
- python3
- SQL
- 데이터 분석
- Numpy
- 튜닝
- level 1
- 빅분기
- 실기
- oracle
- level 2
- 카카오
- 머신러닝
- R
- 파이썬
- 오라클
- 빅데이터 분석 기사
- matplotlib
- 프로그래머스
- 코딩테스트
Archives
- Today
- Total
라일락 꽃이 피는 날
[Oracle] 데이터 분석 함수 3 - 누적 데이터, 비율 본문
728x90
데이터 분석 함수
- sum over : 누적 데이터 출력
- ratio_to_report : 비율 출력
윈도우 기준 | 윈도우 방식 | 설명 |
rows | unbounded preceding | 맨 첫 번째 행을 가리킵니다. |
unbounded following | 맨 마지막 행을 가리킵니다. | |
current row | 현재 행을 가리킵니다. | |
range |
rows 는 행을 기준으로 누적치를 구하는 것이고, range 는 범위를 기준으로 누적치를 구하는 것이다.
예제 1. 사원 번호, 사원 이름, 월급, 사원 테이블의 토탈 월급을 출력
select empno, ename, sal, sum(sal) over()
from emp;
sum(sal) over 다음에 괄호 안에다가 아무것도 쓰지 않으면 전체 토탈 월급이 출력된다.
select empno, ename, sal, sum(sal) over (order by sal rows
between unbounded preceding
and unbounded following) 토탈
from emp;
제일 첫 번째 행부터 제일 마지막 번째 행까지 월급들의 토탈 값을 출력
예제 2. 사원 번호, 사원 이름, 월급, 월급에 대한 누적치를 출력
select empno, ename, sal, sum(sal) over(order by sal)
from emp;
sum(sal) over (order by sal) 까지만 쓰면 14275가 두 번 나온다.
select empno, ename, sal, sum(sal) over (order by sal range
between unbounded preceding
and current row) 누적치
from emp;
sum over 의 기본 옵션이 range between unbounded preceding and current row 으로 되어있다.
select empno, ename, sal, sum(sal) over (order by sal rows
between unbounded preceding
and current row) 누적치
from emp;
제대로 사용하려면 위와 같이 해야 한다.
예제 3. emp2 테이블에서 이름, 입사일, 월급, 월급에 대한 누적치를 출력
create table emp2
as
select *
from emp
order by deptno asc;
update emp2
set hiredate = '81/01/05'
where deptno = 10;
update emp2
set hiredate = '81/02/17'
where deptno = 20;
update emp2
set hiredate = '81/03/21'
where deptno = 30;
commit;
select ename, hiredate, sal, sum(sal) over (order by hiredate range
between unbounded preceding
and current row) 누적치
from emp2;
rows 를 윈도우 기준으로 주었을 때는 무조건 행을 기준으로 월급을 누적하고,
range 를 윈도우 기준으로 주었을 때는 입사일에 대한 범위로 월급을 누적한다.
예제 4. 부서 번호가 20번인 사원들의 사원 번호, 이름, 월급, 월급에 대한 비율을 출력
select empno, ename, sal, ratio_to_report(sal) over () as 비율,
sal / sum(sal) over () as "비교비율"
from emp
where deptno = 20;
예제 5. 부서 번호, 사원 이름, 월급, 자신의 월급의 비율을 출력하는데, 부서 번호 별로 각각 월급의 비율을 출력
select deptno, ename, sal
, ratio_to_report(sal) over (partition by deptno) as 비율
from emp;
728x90
'프로그래밍 > Oracle' 카테고리의 다른 글
[Oracle] 출력되는 행 제한 (0) | 2022.05.31 |
---|---|
[Oracle] 데이터 분석 함수 4 - 집계 결과 출력 (0) | 2022.05.31 |
[Oracle] 데이터 분석 함수 2 - column/row (0) | 2022.05.30 |
[Oracle] 데이터 분석 함수 1 - 순위/등급/넘버링 (0) | 2022.05.30 |
[Oracle] select 문의 6가지 절 (0) | 2022.05.30 |