2021-05-07

该博客介绍了一段SQL查询,用于从44_Project和44_Employee表中获取每个项目的员工平均工作年限,结果精确到小数点后两位。查询使用LEFT JOIN连接两个表,并通过GROUP BY对项目ID进行分组,最后应用AVG函数计算平均经验年限。
摘要由CSDN通过智能技术生成

项目员工

需求一:查询每一个项目中员工的平均工作年限,精确到小数点后两位。

展示效果:

project_idaverage_years
12.00
22.50
Create table If Not Exists 44_Project (project_id int, employee_id int);
Create table If Not Exists 44_Employee (employee_id int, name varchar(10), experience_years int);
Truncate table 44_Project;
insert into 44_Project (project_id, employee_id) values (1, 1);
insert into 44_Project (project_id, employee_id) values (1, 2);
insert into 44_Project (project_id, employee_id) values (1, 3);
insert into 44_Project (project_id, employee_id) values (2, 1);
insert into 44_Project (project_id, employee_id) values (2, 4);
Truncate table 44_Employee;
insert into 44_Employee (employee_id, name, experience_years) values (1, 'Khaled', 3);
insert into 44_Employee (employee_id, name, experience_years) values (2, 'Ali', 2);
insert into 44_Employee (employee_id, name, experience_years) values (3, 'John', 1);
insert into 44_Employee (employee_id, name, experience_years) values (4, 'Doe', 2);

最终SQL:

select 
      project_id ,
      round(avg(experience_years),2) as average_years
from 
      44_Project p
left join
      44_Employee e
on 
      p.employee_id = e.employee_id
group by 
      project_id
order by
      project_id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值