명령어의 처리순서 ( Having )

양혜정·2024년 2월 25일

Oracle

목록 보기
12/49

테이블에서 데이터 정보를 꺼낼 때,

명령어의 처리순서

--------------------------------------------------
select 컬럼명1, 컬럼명2		-> 5 번
from 테이블명 (또는 뷰명)		-> 1 번
where 조건절					-> 2 번
// where 조건절 : 조건에 만족하는 테이블의 행(row)를 
// 			메모리(RAM)에 로딩해주는 것
group by 절					-> 3 번
having 그룹함수조건절			-> 4 번
order by 절 				-> 6 번
--------------------------------------------------

Having 그룹함수조건절

  • group by 의 조건을 나타내고 싶을 때 where 절이 아닌 having 그룹함수조건절을 사용
select department_id as 부서번호
	, to_char(sum(salary), '999,999') as "기본급여의 합계"
from  employees
-- where sum(salary) >= 50000 오류!! 
-- 그룹함수를 where 절 사용 불가
group by department_id
order by 2;

응용

  1. employees 테이블에서 부서번호별로 벌어들이는 월급 합계가 40000 이상인 부서에 대해서만 부서번호, 월급의 합계
select department_id as 부서번호
        , to_char(sum(
        	nvl(salary + (salary * commission_pct),salary)
        				),'999,999') as 월급의합계
   from employees
   group by department_id
   having sum(
  	nvl(salary + (salary * commission_pct),salary))>= 40000
   order by 1;
  1. employees 테이블에서 부서번호별 월급의 합계와 함께 월급합계의 등수(석차)
select to_char(sum(
			nvl(salary + (salary * commission_pct),salary)
				),'999,999') as "월급의 합계"
        , rank() over(order by sum(
        	nvl(salary + (salary * commission_pct),salary)
        			) desc) as 등수
   from employees
   group by department_id;
  1. employees 테이블에서 부서번호별 인원수와 월급의 평균, 월급평균의 등수(석차)
select department_id as 부서번호, count(*) as 인원수
        , to_char(round(avg(
        	nvl(salary + (salary * commission_pct),salary)
        					),0),'99,999') as "월급의 평균"
        , rank() over(order by avg(
        	nvl(salary + (salary * commission_pct),salary)
        							) desc) as 등수
   from employees
   group by department_id
   order by department_id;
  1. employees 테이블에서 모든 사원들이 벌어들이는 월급의 합계를 100% 로 보았을때 각 부서번호별로 벌어들이는 월급의 합계를 % 한 값
select decode(grouping(department_id)
				, 0, nvl(to_char(department_id),'부서없음')
        			, '전체') as 부서번호
                    
    , round(sum(
       nvl(salary + (salary * commission_pct),salary)) 
 		/ (select sum(
       		nvl(salary + (salary * commission_pct),salary)) 
    	   from employees)
		* 100, 1) as "부서별로 총 월급 / 전체월급"
        
from employees
group by rollup(department_id) 
-- rollup 실시 시 전체도 확인 가능
order by 1;
  1. employees 테이블에서 모든 사원들이 벌어들이는 월급의 합계를 100% 로 보았을때 각 부서번호별로 벌어들이는 월급합계의 퍼센티지가 5% 이상인 부서만 부서번호, 월급의합계, 퍼센티지
select department_id as 부서번호
        , to_char(sum(
        nvl(salary + (salary * commission_pct),salary)
        ),'999,999') as 월급의합계
        , round(sum(
        nvl(salary + (salary * commission_pct),salary)) 
       		/ (select sum(
            nvl(salary + (salary * commission_pct),salary)) 
            from employees) * 100, 1)
        as "월급합계의 퍼센티지"
   from employees
   group by department_id
   having sum(
   nvl(salary + (salary * commission_pct),salary)) 
        > (select sum(
        	nvl(salary + (salary * commission_pct),salary))
		   from employees) * 0.05
  order by 1;

참고

그룹함수 : https://velog.io/@jjoung-2j/%EA%B7%B8%EB%A3%B9%ED%95%A8%EC%88%98%EC%A7%91%EA%B3%84%ED%95%A8%EC%88%98


정리

-> local_hr에서작업한것

0개의 댓글