
6. 그룹 함수
그룹함수는 null을 포함하지 않는다(count(*)빼고)
1) count : 행의 수를 구하는 함수
select count(*) -> null을 포함한 행수가 구해짐
from employees
where department_id = 30;
2) max : 최대값 (날짜에서는 최근을 의미함/알파벳순가나다순(Z->A))
3) min : 최소값 (날짜에서는 과거/알파벳순가나다순(A->Z))
select max(salary), min(salary), max(hire_date), min(hire_date),
max(last_name), min(last_name)
from employees;
4) sum : 합 (무조건 숫자형만 가능)
select sum(salary)
from employees
where department_id = 30;
자료의 중심위치를 나타내는 대표값 : 평균, 중앙값, 최빈값(건수)
5) avg : 평균 (null을 포함하지 않음)
select avg(salary)
from employees;
6) median : 중앙값
select median(salary)
from employees;
자료의 퍼진 정도를 측정 : 분산, 표준편차, 범위, 사분위수
7) variance : 분산
select variance(salary)
from employees;
8) stddev : 표준편차
select stddev(salary)
from employees;
9) 범위 : max - min
select max(salary) - min(salary)
from employees;
< group by 절>
: 군집화
1) 그룹화를 먼저하고 집계값이 구해짐
(sort)group by -> (hash)group by로 바뀌어서 정렬이 안되어있음
select department_id, sum(salary)
from employees
group by department_id
order by 1;
2) 그룹함수에 포함되어 있지 않은 개별열이 select절에 나열되어있으면
하나도 빠짐없이 무조건 group by절에 명시해야함.
select department_id, job_id, sum(salary)
from employees
group by department_id, job_id
order by 1;
3) group by 절에는 별칭을 쓸수없다 (오류남)
select department_id as dept_id, job_id, sum(salary)
from employees
group by department_id, job_id
order by 1;
4) 처리 순서때문에 오류발생 / group by절을 제한할때 where을 쓰면 안됨
그래서 나온것이 group함수를 제한하는 having
<having 절>
1) 직계값을 다 만들고 제한 / group를 제한
2) 이해하기 쉽게 group by 뒤에 쓰는 습관을 갖자!
select department_id, sum(salary)
from employees
group by department_id
having sum(salary) >= 10000;
3) group함수를 중첩하게되면 개별 열이 있을시 오류남
select department_id, max(avg(salary))
from employees
group by department_id;
이건 됨
select max(avg(salary))
from employees
group by department_id;
department_id도 쓰고 싶다면 서브쿼리를 쓰면됨
'컴퓨터 > SQL' 카테고리의 다른 글
VMware workstation 15 설치방법 (0) | 2020.02.06 |
---|---|
오라클 SQL JOIN문 정리 (0) | 2020.01.30 |
오라클 SQL 함수 정리 - ⑤ 일반 함수 (0) | 2020.01.28 |
오라클 SQL 함수 정리 - ③ 날짜 함수, ④ 형변환 함수 (0) | 2020.01.28 |
오라클 SQL 함수 정리 - ② 숫자 함수 (0) | 2020.01.28 |