返回介绍

solution / 3000-3099 / 3057.Employees Project Allocation / README_EN

发布于 2024-06-17 01:02:57 字数 5468 浏览 0 评论 0 收藏 0

3057. Employees Project Allocation

中文文档

Description

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 t_he 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.

Solutions

Solution 1: Grouping Statistics + Equi-Join

First, we join the Project table and the Employees table based on employee_id, then group by team to calculate the average workload of each team, and record it in the temporary table T.

Then, we join the Project table and the Employees table again, and also join the T table, to find employees whose workload is greater than the average workload of the team. Finally, we sort by employee_id and project_id.

# Write your MySQL query statement below
WITH
  T AS (
    SELECT team, AVG(workload) AS avg_workload
    FROM
      Project
      JOIN Employees USING (employee_id)
    GROUP BY 1
  )
SELECT
  employee_id,
  project_id,
  name AS employee_name,
  workload AS project_workload
FROM
  Project
  JOIN Employees USING (employee_id)
  JOIN T USING (team)
WHERE workload > avg_workload
ORDER BY 1, 2;
import pandas as pd


def employees_with_above_avg_workload(
  project: pd.DataFrame, employees: pd.DataFrame
) -> pd.DataFrame:
  merged_df = pd.merge(project, employees, on="employee_id")
  avg_workload_per_team = merged_df.groupby("team")["workload"].mean().reset_index()
  merged_df = pd.merge(
    merged_df, avg_workload_per_team, on="team", suffixes=("", "_avg")
  )
  ans = merged_df[merged_df["workload"] > merged_df["workload_avg"]]
  ans = ans[["employee_id", "project_id", "name", "workload"]]
  ans = ans.rename(columns={"name": "employee_name", "workload": "project_workload"})
  return ans.sort_values(by=["employee_id", "project_id"])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文