-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 2.sql
More file actions
67 lines (58 loc) · 1.51 KB
/
Task 2.sql
File metadata and controls
67 lines (58 loc) · 1.51 KB
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
# Task 2
# Compare the number of male managers to the number of female managers from different departments for each year, starting from 1990.
# What chart suits this question best? Area Chart with gender split
# Problem 2: Solution in SQL with the use of subquery
SELECT
d.dept_name,
ee.gender,
dm.emp_no,
dm.from_date,
dm.to_date,
e.calendar_year,
CASE
WHEN
YEAR(dm.to_date) >= e.calendar_year
AND YEAR(dm.from_date) <= e.calendar_year
THEN
1
ELSE 0
END AS active
FROM
(SELECT
YEAR(hire_date) AS calendar_year
FROM
employees
GROUP BY calendar_year) e
CROSS JOIN
dept_manager dm
JOIN
departments d ON dm.dept_no = d.dept_no
JOIN
employees ee ON dm.emp_no = ee.emp_no
ORDER BY dm.emp_no , calendar_year;
# Data verfication
SELECT * FROM (SELECT
YEAR(hire_date) AS calendar_year
FROM
employees
GROUP BY calendar_year) e
CROSS JOIN
dept_manager dm
JOIN
departments d ON dm.dept_no = d.dept_no
JOIN
employees ee ON dm.emp_no = ee.emp_no
ORDER BY dm.emp_no , calendar_year;
SELECT
YEAR(hire_date) AS calendar_year
FROM
employees
GROUP BY calendar_year;
CREATE TABLE calendar_year (zone YEAR);
INSERT INTO calendar_year (SELECT
YEAR(hire_date) AS calendar_year
FROM
employees
GROUP BY calendar_year);
SELECT * FROM employees.calendar_year;
DROP TABLE calendar_year;