1. 세로 연산의 원리
그룹함수를 쓰면 세로 연산이 된다. 세로연산이 되면, 결국 새로운 하나의 룰을 만들어낸다.
새로운 줄에, 다른 칼럼이 표현되고 싶으면, 값이 같으면 스쿼시 된다.
2. Group by해서 연산하기
- 전체행을 세로연산 하려면 무엇을 써? - sum, max, min, count, avg(그룹함수)
- 특정행들을 세로연산 하려면 무엇을 써? - where과 그룹함수
- 특정 그룹만 세로연산 하려면 무엇을 써? - where과 그룹함수
- 그룹별로 세로연산 하려면 무엇을 써? - group by와 그룹함수
Group by, Union All
-- 세로 연산
-- 1. 그룹 함수
select avg(height)
from student;
select count(height)
from student;
select sum(height)
from student;
select min(height)
from student;
select max(height)
from student;2. 원하는 행만 세로 연산
-- 2. 원하는 행만 세로 연산하기
select avg(weight)
from student
where year(birthday) = 1975;
desc student;
select floor(avg(pay)) "정교수 평균연봉"
from professor
where position = "정교수";3. 그룹핑의 이해
-- 3. 그룹핑 하기
select avg(sal), deptno
from emp
where deptno = 10
union all -- 행 붙이기
select avg(sal), deptno
from emp
where deptno = 20
union all
select avg(sal), deptno
from emp
where deptno = 30;
select avg(sal)
from emp
group by deptno;


4. 그룹핑 원리 실습
-- 4. 그룹핑 원리 실습
select *
from student;
select avg(height), grade
from student
group by grade;
select job, deptno, avg(sal)
from emp
group by job, deptno;
-- 4. 그룹핑 원리 실습 / 특정 그룹 제거
-- 그룹으로 다 묶은 뒤 특정 그룹 제거 필요 시 유용
select *
from student;
select avg(height), grade
from student
group by grade;
select job, deptno, avg(sal)
from emp
group by job, deptno
having deptno != 10; -- deptno의 10번 그룹 제거 할 수 있음
🎀having 활용
-- 평균 연봉이 2000 이상인 부서는?
select avg(sal), deptno
from emp
group by deptno
having avg(sal) > 2000;Share article