반응형






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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Group by 절 */
 
select distinct(department_id) 
from employees;
 
 
select department_id 
from employees 
group by department_id;
 
 
 
/* 부서 별 급여 합계 */
 
select department_id, sum(salary) 
from employees 
group by department_id;
 
 
 
/* 단일 그룹이 아니므로 에러 */
 
select distinct(department_id), sum(salary) 
from employees; 
 
 
 
/* 부서 별 사원수와 급여 평균 */
 
select department_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id;
 
 
 
/*  부서별 직급별 사원수와 급여 평균 */
 
select department_id, job_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id, job_id
order by department_id, job_id;
 
 
 
select department_id, job_id,
   to_char(sum(salary),'999,999'"총급여",
   to_char(avg(salary),'999,999'"평균급여"
from employees
where department_id = 80
group by department_id, job_id
order by department_id, job_id;
 
 
 
/* 현재 부서 별 사원수 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id;
 
 
 
/* Where절에서는 집계함수 사용 불가 */
 
select department_id, count(*)
from employees
where department_id is not null
    and count(*< 10
group by department_id;  
 
 
 
/* Having 절 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id
Having count(*>=10;
 
 
 
/* Rollup : 그룹별 합계 정보를 추가해 보여주는 함수 */
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
  and d.location_id = l.location_id
group by l.city, d.department_name, e.job_id
order by l.city, d.department_name, e.job_id;
 
 
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
    and d.location_id = l.location_id
group by rollup(l.city, d.department_name, e.job_id)
order by l.city, d.department_name, e.job_id;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03

+ Recent posts