通知
关于网站更多信息请加whimurmur模板/jpress插件QQ群(1061691290)            网站从因情语写改为晴雨            这个网站的模板也从calmlog_ex改为 whimurmur

leetcode数据库 系列题 项目员工

596人浏览 / 0人评论 / | 作者:因情语写  | 分类: 数据库  | 标签: 数据库  /  leetcode  | 

作者:因情语写

链接:https://www.proprogrammar.com/article/650

声明:请尊重原作者的劳动,如需转载请注明出处


    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
select p.project_id, round(avg(e.experience_years), 2) average_years
from Project p inner join Employee e
on p.employee_id = e.employee_id
group by p.project_id

    两表连接分组求平均,学一下group by+聚集函数,xxx join + on

    1076. 项目员工II

Table: Project

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

Table: Employee

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

编写一个SQL查询,报告所有雇员最多的项目。

查询结果格式如下所示:

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

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

Result table:
+-------------+
| project_id  |
+-------------+
| 1           |
+-------------+
第一个项目有3名员工,第二个项目有2名员工。
select p.project_id
from(
    select count(employee_id) empNum
    from Project
    group by project_id
    order by empNum desc
    limit 1
) pCount, (
    select project_id, count(employee_id) empNum
    from Project
    group by project_id
) p
where pCount.empNum = p.empNum

     from中两个子查询连接在一起,学一下group by + order by + limit 1求最值,求员工数等于最大员工数的项目

    下面看另一种解法

SELECT project_id from project group by project_id having count(employee_id) >=(
SELECT count(employee_id) from project group by project_id order by count(employee_id) DESC LIMIT 1 )

    核心差不多,求所有项目中的最多员工数,这里把上面的连接换成了having子句,学一下having+聚集函数+单列子查询的用法,学一下group by + order by + limit 1求最值的用法

    1077. 项目员工 III

项目表 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   | 3                |
| 4           | Doe    | 2                |
+-------------+--------+------------------+

Result 表:
+-------------+---------------+
| project_id  | employee_id   |
+-------------+---------------+
| 1           | 1             |
| 1           | 3             |
| 2           | 1             |
+-------------+---------------+
employee_id 为 1 和 3 的员工在 project_id 为 1 的项目中拥有最丰富的经验。在 project_id 为 2 的项目中,employee_id 为 1 的员工拥有最丰富的经验。
select p.project_id, e2.employee_id
from(
    select project_id, max(e.experience_years) maxYear
    from Project p, Employee e
    where p.employee_id = e.employee_id
    group by project_id
) p2 inner join Employee e2 on e2.experience_years = p2.maxYear
inner join Project p on e2.employee_id = p.employee_id and p.project_id = p2.project_id

    先求出每个项目员工的最大经验年限,找出该经验年限员工,且员工要在当前项目中,可能两个inner join位置调换一下比较好理解,但这也体现了灵活的地方,只要最后结果对,顺序没影响,不一定要符合思维习惯

    下面看一下另一种解法

SELECT project_id,A.employee_id
FROM Project A LEFT JOIN Employee B
ON A.employee_id=B.employee_id
WHERE (project_id,experience_years)
IN
(SELECT project_id,MAX(experience_years)
FROM Project LEFT JOIN Employee
ON Project.employee_id=Employee.employee_id

GROUP BY project_id)

     用多字段in求最值,更加简洁,学一下多字段in求最值的用法


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!
广告位-帮帮忙点下广告