7일차 # 4-4: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE ...
- 사원의 총급여
select sum(sal)
from emp;
- 부서별 급여합 구하기
select sum(sal)
from emp
where deptno = 10
union all
select sum(sal)
from emp
where deptno = 20
union all
select sum(sal)
from emp
where deptno = 30;
↓ ↓ ↓
- GROUP BY 절 사용
select sum(sal)
from emp
group by deptno;
select deptno, sum(sal)
from emp; --> 오류: ORA-00937: 단일 그룹의 그룹 함수가 아닙니다
===> select-list에 단일 컬럼 또는 수식을 그룹 함수와 같이 사용하려면
반드시 group by 절에 해당 단일 컬럼 또는 수식을 명시해 주어야 합니다.
↓ ↓ ↓
select deptno, sum(sal)
from emp
group by deptno;
select substr(ename, -1, 1), sum(sal)
from emp
group by ename; --> 엉터리
select substr(ename, -1, 1), sum(sal)
from emp
group by substr(ename, -1, 1); --> 제대로
* 그러나 group by 절에 명시되었다고 반드시 select-list 에 명시될 필요는 없습니다.
select sum(sal)
from emp
group by deptno;
===> group by 절의 단일컬럼 또는 수식은 반드시 select-list에
명시될 필요는 없습니다.
select deptno, job, sum(sal)
from emp
group by deptno, job;
select to_char(hiredate, 'YYYY') as Year, sum(sal)
from emp
group by to_char(hiredate, 'YYYY');
==> 9i 까지는 sort group by가 수행되었으나
10g부터는 hash group by가 수행됩니다. ==>