테이블에서 데이터 정보를 꺼낼 때,
명령어의 처리순서
-------------------------------------------------- select 컬럼명1, 컬럼명2 -> 5 번 from 테이블명 (또는 뷰명) -> 1 번 where 조건절 -> 2 번 // where 조건절 : 조건에 만족하는 테이블의 행(row)를 // 메모리(RAM)에 로딩해주는 것 group by 절 -> 3 번 having 그룹함수조건절 -> 4 번 order by 절 -> 6 번 --------------------------------------------------
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;
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;
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;
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;
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;
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에서작업한것