@@ -5,6 +5,7 @@ drop function if exists emp_dept_id // |
5 | 5 | drop function if exists emp_dept_name // |
6 | 6 | drop function if exists emp_name // |
7 | 7 | drop function if exists current_manager // |
| 8 | +drop procedure if exists show_departments // |
8 | 9 |
|
9 | 10 | -- |
10 | 11 | -- returns the department id of a given employee |
@@ -15,22 +16,22 @@ reads sql data |
15 | 16 | begin |
16 | 17 | declare max_date date; |
17 | 18 | set max_date = ( |
18 | | - select |
19 | | - max(from_date) |
20 | | - from |
21 | | - dept_emp |
22 | | - where |
| 19 | + select |
| 20 | + max(from_date) |
| 21 | + from |
| 22 | + dept_emp |
| 23 | + where |
23 | 24 | emp_no = employee_id |
24 | 25 | ); |
25 | 26 | set @max_date=max_date; |
26 | 27 | return ( |
27 | | - select |
| 28 | + select |
28 | 29 | dept_no |
29 | | - from |
| 30 | + from |
30 | 31 | dept_emp |
31 | | - where |
| 32 | + where |
32 | 33 | emp_no = employee_id |
33 | | - and |
| 34 | + and |
34 | 35 | from_date = max_date |
35 | 36 | limit 1 |
36 | 37 | ); |
|
47 | 48 | return ( |
48 | 49 | select |
49 | 50 | dept_name |
50 | | - from |
| 51 | + from |
51 | 52 | departments |
52 | 53 | where |
53 | 54 | dept_no = emp_dept_id(employee_id) |
@@ -62,11 +63,11 @@ returns varchar(32) |
62 | 63 | reads SQL data |
63 | 64 | begin |
64 | 65 | return ( |
65 | | - select |
| 66 | + select |
66 | 67 | concat(first_name, ' ', last_name) as name |
67 | | - from |
| 68 | + from |
68 | 69 | employees |
69 | | - where |
| 70 | + where |
70 | 71 | emp_no = employee_id |
71 | 72 | ); |
72 | 73 | end// |
@@ -82,22 +83,22 @@ reads sql data |
82 | 83 | begin |
83 | 84 | declare max_date date; |
84 | 85 | set max_date = ( |
85 | | - select |
86 | | - max(from_date) |
87 | | - from |
88 | | - dept_manager |
89 | | - where |
| 86 | + select |
| 87 | + max(from_date) |
| 88 | + from |
| 89 | + dept_manager |
| 90 | + where |
90 | 91 | dept_no = dept_id |
91 | 92 | ); |
92 | 93 | set @max_date=max_date; |
93 | 94 | return ( |
94 | | - select |
| 95 | + select |
95 | 96 | emp_name(emp_no) |
96 | | - from |
| 97 | + from |
97 | 98 | dept_manager |
98 | | - where |
| 99 | + where |
99 | 100 | dept_no = dept_id |
100 | | - and |
| 101 | + and |
101 | 102 | from_date = max_date |
102 | 103 | limit 1 |
103 | 104 | ); |
@@ -112,16 +113,16 @@ delimiter ; |
112 | 113 |
|
113 | 114 | CREATE OR REPLACE VIEW v_full_employees |
114 | 115 | AS |
115 | | -SELECT |
116 | | - emp_no, |
117 | | - first_name , last_name , |
118 | | - birth_date , gender, |
| 116 | +SELECT |
| 117 | + emp_no, |
| 118 | + first_name , last_name , |
| 119 | + birth_date , gender, |
119 | 120 | hire_date, |
120 | | - emp_dept_name(emp_no) as department |
121 | | -from |
| 121 | + emp_dept_name(emp_no) as department |
| 122 | +from |
122 | 123 | employees; |
123 | 124 |
|
124 | | --- |
| 125 | +-- |
125 | 126 | -- selects the department list with manager names |
126 | 127 | -- |
127 | 128 |
|
@@ -138,7 +139,6 @@ delimiter // |
138 | 139 | -- shows the departments with the number of employees |
139 | 140 | -- per department |
140 | 141 | -- |
141 | | -drop procedure if exists show_departments // |
142 | 142 | create procedure show_departments() |
143 | 143 | modifies sql data |
144 | 144 | begin |
@@ -153,36 +153,73 @@ begin |
153 | 153 | ); |
154 | 154 | INSERT INTO department_max_date |
155 | 155 | SELECT |
156 | | - emp_no, max(from_date), max(to_date) |
| 156 | + emp_no, max(from_date), max(to_date) |
157 | 157 | FROM |
158 | 158 | dept_emp |
159 | 159 | GROUP BY |
160 | 160 | emp_no; |
161 | 161 |
|
162 | | - CREATE TEMPORARY TABLE department_people |
| 162 | + CREATE TEMPORARY TABLE department_people |
163 | 163 | ( |
164 | 164 | emp_no int not null, |
165 | 165 | dept_no char(4) not null, |
166 | 166 | primary key (emp_no, dept_no) |
167 | 167 | ); |
168 | 168 |
|
169 | | - insert into department_people |
| 169 | + insert into department_people |
170 | 170 | select dmd.emp_no, dept_no |
171 | | - from |
172 | | - department_max_date dmd |
173 | | - inner join dept_emp de |
174 | | - on dmd.dept_from_date=de.from_date |
| 171 | + from |
| 172 | + department_max_date dmd |
| 173 | + inner join dept_emp de |
| 174 | + on dmd.dept_from_date=de.from_date |
175 | 175 | and dmd.dept_to_date=de.to_date |
176 | 176 | and dmd.emp_no=de.emp_no; |
177 | | - SELECT |
178 | | - dept_no,dept_name,manager, count(*) |
179 | | - from v_full_departments |
180 | | - inner join department_people using (dept_no) |
| 177 | + SELECT |
| 178 | + dept_no,dept_name,manager, count(*) |
| 179 | + from v_full_departments |
| 180 | + inner join department_people using (dept_no) |
181 | 181 | group by dept_no; |
182 | 182 | # with rollup; |
183 | 183 | DROP TABLE department_max_date; |
184 | 184 | DROP TABLE department_people; |
185 | 185 | end // |
186 | 186 |
|
| 187 | +drop function if exists employees_usage // |
| 188 | +drop procedure if exists employees_help // |
| 189 | + |
| 190 | +CREATE FUNCTION employees_usage () |
| 191 | +RETURNS TEXT |
| 192 | +DETERMINISTIC |
| 193 | +BEGIN |
| 194 | + RETURN |
| 195 | +' |
| 196 | + == USAGE == |
| 197 | + ==================== |
| 198 | +
|
| 199 | + PROCEDURE show_departments() |
| 200 | +
|
| 201 | + shows the departments with the manager and |
| 202 | + number of employees per department |
| 203 | +
|
| 204 | + FUNCTION current_manager (dept_id) |
| 205 | +
|
| 206 | + Shows who is the manager of a given departmennt |
| 207 | +
|
| 208 | + FUNCTION emp_name (emp_id) |
| 209 | +
|
| 210 | + Shows name and surname of a given employee |
| 211 | +
|
| 212 | + FUNCTION emp_dept_id (emp_id) |
| 213 | +
|
| 214 | + Shows the current department of given employee |
| 215 | +'; |
| 216 | +END // |
| 217 | + |
| 218 | +create procedure employees_help() |
| 219 | +deterministic |
| 220 | +begin |
| 221 | + select employees_usage() as info; |
| 222 | +end// |
| 223 | + |
187 | 224 | delimiter ; |
188 | 225 |
|