-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3057-EmployeesProjectAllocation.sql
More file actions
117 lines (111 loc) · 5.23 KB
/
3057-EmployeesProjectAllocation.sql
File metadata and controls
117 lines (111 loc) · 5.23 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- 3057. Employees Project Allocation
-- Table: Project
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | project_id | int |
-- | employee_id | int |
-- | workload | int |
-- +-------------+---------+
-- employee_id is the primary key (column with unique values) of this table.
-- employee_id is a foreign key (reference column) to Employee table.
-- Each row of this table indicates that the employee with employee_id is working on the project with project_id and the workload of the project.
-- Table: Employees
-- +------------------+---------+
-- | Column Name | Type |
-- +------------------+---------+
-- | employee_id | int |
-- | name | varchar |
-- | team | varchar |
-- +------------------+---------+
-- employee_id is the primary key (column with unique values) of this table.
-- Each row of this table contains information about one employee.
-- Write a solution to find the employees who are allocated to projects with a workload that exceeds the average workload of all employees for their respective teams
-- Return the result table ordered by employee_id, project_id in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Project table:
-- +-------------+-------------+----------+
-- | project_id | employee_id | workload |
-- +-------------+-------------+----------+
-- | 1 | 1 | 45 |
-- | 1 | 2 | 90 |
-- | 2 | 3 | 12 |
-- | 2 | 4 | 68 |
-- +-------------+-------------+----------+
-- Employees table:
-- +-------------+--------+------+
-- | employee_id | name | team |
-- +-------------+--------+------+
-- | 1 | Khaled | A |
-- | 2 | Ali | B |
-- | 3 | John | B |
-- | 4 | Doe | A |
-- +-------------+--------+------+
-- Output:
-- +-------------+------------+---------------+------------------+
-- | employee_id | project_id | employee_name | project_workload |
-- +-------------+------------+---------------+------------------+
-- | 2 | 1 | Ali | 90 |
-- | 4 | 2 | Doe | 68 |
-- +-------------+------------+---------------+------------------+
-- Explanation:
-- - Employee with ID 1 has a project workload of 45 and belongs to Team A, where the average workload is 56.50. Since his project workload does not exceed the team's average workload, he will be excluded.
-- - Employee with ID 2 has a project workload of 90 and belongs to Team B, where the average workload is 51.00. Since his project workload does exceed the team's average workload, he will be included.
-- - Employee with ID 3 has a project workload of 12 and belongs to Team B, where the average workload is 51.00. Since his project workload does not exceed the team's average workload, he will be excluded.
-- - Employee with ID 4 has a project workload of 68 and belongs to Team A, where the average workload is 56.50. Since his project workload does exceed the team's average workload, he will be included.
-- Result table orderd by employee_id, project_id in ascending order.
-- Create table If Not Exists Project (project_id int, employee_id int, workload int)
-- Create table If Not Exists Employees (employee_id int, name varchar(20), team varchar(20))
-- Truncate table Project
-- insert into Project (project_id, employee_id, workload) values ('1', '1', '45')
-- insert into Project (project_id, employee_id, workload) values ('1', '2', '90')
-- insert into Project (project_id, employee_id, workload) values ('2', '3', '12')
-- insert into Project (project_id, employee_id, workload) values ('2', '4', '68')
-- Truncate table Employees
-- insert into Employees (employee_id, name, team) values ('1', 'Khaled', 'A')
-- insert into Employees (employee_id, name, team) values ('2', 'Ali', 'B')
-- insert into Employees (employee_id, name, team) values ('3', 'John', 'B')
-- insert into Employees (employee_id, name, team) values ('4', 'Doe', 'A')
WITH t AS ( -- 汇总信息
SELECT
p.project_id,
p.workload,
e.*
FROM
Employees AS e
LEFT JOIN
Project AS p
ON
e.employee_id = p.employee_id
),
a AS (
SELECT AVG(workload) AS avg_workload,team FROM t GROUP BY team
)
-- SELECT * FROM t
-- | project_id | workload | employee_id | name | team |
-- | ---------- | -------- | ----------- | ------ | ---- |
-- | 1 | 45 | 1 | Khaled | A |
-- | 1 | 90 | 2 | Ali | B |
-- | 2 | 12 | 3 | John | B |
-- | 2 | 68 | 4 | Doe | A |
-- 每个团队的平均工作量
-- SELECT AVG(workload) AS avg_workload FROM t GROUP BY team
-- | avg_workload |
-- | ------------- |
-- | 56.5 |
-- | 51 |
SELECT
t.employee_id,
t.project_id,
t.name AS employee_name,
t.workload AS project_workload
FROM
t
JOIN
a
ON
t.team = a.team AND t.workload > a.avg_workload -- 分配给项目的工作量 超过各自团队 所有员工 平均工作量 的 员工
ORDER BY
t.employee_id,t.project_id -- 以 employee_id,project_id 升序 排序