group by : 그룹을 만들어서, 그룹 안에서 계산한다.
- group by 뒤에 오는 속성을 기준으로 그룹을 만든다.
- group by 뒤에 오는 속성은 ,(comma)를 사용하여 여러 개가 올 수 있다.
주의* group by는 group 함수와 함께 사용!!
- group 함수 : max, min, sum, avg, count
주의* group by 위치는 order by의 앞 order by는 맨 뒤로 온다. --조회된 결과를 정렬하기 때문에
주의* group by의 select절에는 group 함수와 group by 뒤에 오는 속성만 온다.
-- 단일행 함수 : 수행 결과가 단일행
-- 그룹 함수 : 수행 결과가 다중행의 결과
예시) select max(sal) from emp; -- 그룹함수
select job, max(sal),min(sal),sum(sal),trunc(avg(sal), 2),count(sal) from emp group by job;
select deptno, max(sal),min(sal),sum(sal),trunc(avg(sal), 2),count(sal) from emp group by deptno;
select job, max(sal),min(sal),sum(sal),trunc(avg(sal), 2),count(sal) from emp group by job;
select deptno, max(sal),min(sal),sum(sal),trunc(avg(sal), 2),count(sal) from emp group by deptno order by deptno asc;
select min(sal) from emp;
select sum(sal) from emp;
select avg(sal) from emp;
select count(sal) from emp;
select max(sal) from emp; -- 그룹함수
select min(sal) from emp; -- emp table 14명에 대한 계산 결과
select sum(sal) from emp group by deptno;--세개 그룹의 결과 세개.
--group by deptno : 10, 20, 30 그룹 별로 계산해라.
select deptno, sum(sal) from emp group by deptno;
--select 절에 그룹의 기준을 넣을 수 있다.
--having 절에도 그룹의 기준과 그룹 함수만 온다.
having
- group by의 조건절을 만드는 명령어
- select문에서 where의 역할과 비슷
- group by의 결과에 대해서 where처럼 출력 결과에 조건을 건다.
- having deptno = 10; having sum(sal) > 9000;
- having + 조건기준속성 + 비교연산자(조건연산자) + 조건비교값(기준데이터)
- 조건기준속성 : group 함수와, group by의 기준 속성만 온다.
- group by의 select 정에는 group 함수와 group by의 기준 속성만 온다.
- and, or를 통해 조건을 확장 할 수 있다.
- where처럼 조건을 제한하는 역할
- group by 된 조회 결과에 대해서, where처럼 조건을 제한하는 역할
- having + 기준속성 조건연산자 + 기준데이터
- having 뒤에는 그룹함수와 group by의 기준 속성만 온다.
예시)
select deptno, sum(sal) from emp group by deptno having sum(sal) > 9000;
select deptno, sum(sal) from emp group by deptno
having sum(sal) > 9000 and deptno = 20;
select deptno, sum(sal) from emp group by deptno
having sum(sal) > 9000 or deptno = 10;
'Oracle SQLD' 카테고리의 다른 글
constraints (0) | 2022.06.03 |
---|---|
table (0) | 2022.06.03 |
nvl, nvl2, decode, case ~ when ~ then (0) | 2022.06.03 |
to_date, months_between, add_months, next_day, last_day, 날짜 반올림과 자르기 (0) | 2022.06.03 |
to_char, data type (0) | 2022.05.25 |