본문 바로가기
2019/ORACLE

SQL group by (문제)

by SOLYI 2019. 11. 18.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--부서별 연도별 01~03 04~06 / 07~나머지년도 인원을 계산하세요.
--      -연도(column)
--부서(row)
select department_id, count(*),
    count(decode(to_char(hire_date,'yyyy'), 200112002120031)) as "1~3",
    count(decode(to_char(hire_date,'yyyy'), 200412005120061)) as "4~6",
    count(decode(to_char(hire_date,'yyyy'), 200712008120091)) as "7~"
from employees  group by department_id  order by department_id asc;
 
--부서20,50,80,90에 대해 직무, 부서에 해당하는 급여 및 해당직무에 대한 총 급여를 표시하도록한다.
--job dept20 dept50... total
--ac_mgr nul nul nul ...
--st_man null 58000...
--job_id별로, 
 
select job_id,
sum(decode(department_id, 20, salary)) as dept20,
sum(decode(department_id, 50, salary)) as dept50,
sum(decode(department_id, 80, salary)) as dept80,
sum(decode(department_id, 90, salary)) as dept90,
 
--sum(decode(department_id, 20,salary,50,salary,80,salary,90,salary)) as total
sum(salary) as total
from employees  
where department_id in(20,50,80,90)
group by job_id;
 
select job_id,
sum(decode(department_id, 20, salary)) as dept20,
sum(decode(department_id, 50, salary)) as dept50,
sum(decode(department_id, 80, salary)) as dept80,
sum(decode(department_id, 90, salary)) as dept90,
sum(decode(department_id, 20,salary,50,salary,80,salary,90,salary)) as total
from employees  
group by job_id;
cs
반응형

'2019 > ORACLE' 카테고리의 다른 글

정규화 개념 / 키 / 제1~3정규화 / 비정규화  (0) 2019.11.19
SQL JOIN equi / non-equi / self / outer / inner  (0) 2019.11.18
SQL group by having  (0) 2019.11.18
SQL sum avg count max min  (0) 2019.11.18
SQL CASE-WHEN-THEN  (0) 2019.11.15