-
oracle group by , having , rollupDataBase/oracle 2020. 3. 15. 21:45123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263/* group by 절 */-- distinct로 중복제거select distinct department_id from employees;-- group by로 묶어서 출력select department_id from employees group by department_id;-- 부서별 급여 합계select department_id, sum(salary) from employees group by department_id;select distnict department_id, sum(salary) from employees; -- 에러-- 부서별 급여 합계 인원수 평균select department_id , sum(salary), count(salary), trunc(avg(salary),2) from employees group by department_id;-- 부서별, 직급별 사원수와 평균 급여select department_id , job_id,sum(salary), count(salary), trunc(avg(salary),2) from employees group by department_id, job_id;-- order by 추가select department_id , job_id,sum(salary), count(salary), trunc(avg(salary),2)from employeesgroup by department_id, job_idorder by department_id, job_id;select department_id, job_id,to_char(sum(salary), '999,999') 총급여, -- 형식을 지정할 수 있다!!!to_char(avg(salary), '999,999') 평균급여from employeeswhere department_id = 80GROUP by department_id, job_idorder by department_id, job_id;/* HAVING() 절 */-- 부서별 사원수select department_id, count(*) from employeeswhere department_id is not nullGROUP by department_id;-- 사원수가 10명 이상인 부서--(1) 에러select department_id, count(*) from employeeswhere department_id is not nulland count(*)>10 -- where 절에서는 집계함수를 사용할 수 없다GROUP by department_id;--(2) having 사용 (보통은 group by 절 뒤에 사용하지만 앞에와서 상관은없다)select department_id, count(*) from employeeswhere department_id is not nullGROUP by department_idhaving count(*) >= 10 ;/* rollup() 그룹별 합계 정보를 추가해서 보여줌 */SELECT l.city, d.department_name, e.job_id,count(*) 사원수, sum(e.salary) 총급여from employees e, departments d, locations lwhere e.department_id = d.department_idand d.location_id = l.location_idgroup by ROLLUP( l.city, d.department_name, e.job_id) -- 부서별 합계(인원수/급여)가 출력된다order by l.city, d.department_name, e.job_id;http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
** rollup 적용 전
** rollup 적용 후 ( 도시별/부서별/직급을 출력하고 도시별/부서별의 사원수 합계 총 급여 합계를 보여준다)
'DataBase > oracle' 카테고리의 다른 글
oracle subQuery (0) 2020.03.19 oracle join (0) 2020.03.16 oracle 날짜함수, 변환함수, decode, case (0) 2020.03.15 oracle 문자형 함수 (0) 2020.03.15 oracle 숫자형 함수 (0) 2020.03.15