列表

详情


1075. 项目员工 I

项目表 Project: 

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| project_id  | int     |
| employee_id | int     |
+-------------+---------+
主键为 (project_id, employee_id)。
employee_id 是员工表 Employee 表的外键。

员工表 Employee

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| employee_id      | int     |
| name             | varchar |
| experience_years | int     |
+------------------+---------+
主键是 employee_id。

 

请写一个 SQL 语句,查询每一个项目中员工的 平均 工作年限,精确到小数点后两位

查询结果的格式如下:

Project 表:
+-------------+-------------+
| project_id  | employee_id |
+-------------+-------------+
| 1           | 1           |
| 1           | 2           |
| 1           | 3           |
| 2           | 1           |
| 2           | 4           |
+-------------+-------------+

Employee 表:
+-------------+--------+------------------+
| employee_id | name   | experience_years |
+-------------+--------+------------------+
| 1           | Khaled | 3                |
| 2           | Ali    | 2                |
| 3           | John   | 1                |
| 4           | Doe    | 2                |
+-------------+--------+------------------+

Result 表:
+-------------+---------------+
| project_id  | average_years |
+-------------+---------------+
| 1           | 2.00          |
| 2           | 2.50          |
+-------------+---------------+
第一个项目中,员工的平均工作年限是 (3 + 2 + 1) / 3 = 2.00;第二个项目中,员工的平均工作年限是 (3 + 2) / 2 = 2.50

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

pythondata 解法, 执行用时: 384 ms, 内存消耗: 67.2 MB, 提交时间: 2024-05-27 11:27:55

import pandas as pd

def project_employees_i(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
    # 执行左连接
    merged_df = pd.merge(project, employee, on='employee_id', how='left')
    
    # 计算每个项目的平均经验年限并四舍五入到2位小数
    res = merged_df.groupby(['project_id'], as_index=False)['experience_years'].mean().round(2)
    
    # 重命名结果列名为'average_years'
    return res.rename(columns={'experience_years': 'average_years'})
    

# 
def project_employees_i2(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
  ans = pd.merge(project, employee, on='employee_id')
  return ans[['project_id', 'experience_years']].groupby('project_id').mean().reset_index(drop=False).rename(columns={'experience_years': 'average_years'}).round(2)

mysql 解法, 执行用时: 575 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:50:19

# Write your MySQL query statement below
select project_id, round(avg(experience_years), 2) as average_years
from Project as P join Employee as E
on P.employee_id = E.employee_id
group by P.project_id

mysql 解法, 执行用时: 552 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:49:57

# Write your MySQL query statement below
SELECT project_id AS 'project_id', 
    ROUND(AVG(experience_years), 2) AS 'average_years'
FROM Project AS p1
    INNER JOIN Employee AS e1
    ON p1.employee_id = e1.employee_id
GROUP BY p1.project_id

mysql 解法, 执行用时: 542 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:49:46

# Write your MySQL query statement below
select
    p.project_id,
    round(sum(experience_years)/count(1),2) as average_years
from Project p
left join Employee e
on p.employee_id = e.employee_id
group by p.project_id

mysql 解法, 执行用时: 562 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:49:06

# Write your MySQL query statement below
select
    project_id,
    round(avg(experience_years),2) as average_years 
from project
join employee
using(employee_id)
group by project_id;

上一题