Skip to content

Latest commit

 

History

History

1303.Find the Team Size

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

English Version

题目描述

员工表:Employee

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| employee_id   | int     |
| team_id       | int     |
+---------------+---------+
employee_id 字段是这张表的主键(具有唯一值的列)
表中的每一行都包含每个员工的 ID 和他们所属的团队。

 

编写解决方案以求得每个员工所在团队的总人数。

返回结果表 无顺序要求 

返回结果格式示例如下:

 

示例 1:

输入:
Employee Table:
+-------------+------------+
| employee_id | team_id    |
+-------------+------------+
|     1       |     8      |
|     2       |     8      |
|     3       |     8      |
|     4       |     7      |
|     5       |     9      |
|     6       |     9      |
+-------------+------------+
输出:
+-------------+------------+
| employee_id | team_size  |
+-------------+------------+
|     1       |     3      |
|     2       |     3      |
|     3       |     3      |
|     4       |     1      |
|     5       |     2      |
|     6       |     2      |
+-------------+------------+
解释:
ID 为 1、2、3 的员工是 team_id 为 8 的团队的成员,
ID 为 4 的员工是 team_id 为 7 的团队的成员,
ID 为 5、6 的员工是 team_id 为 9 的团队的成员。

解法

SQL

解法 1:

# Write your MySQL query statement below
SELECT
    e.employee_id,
    t.team_size
FROM
    Employee AS e
    LEFT JOIN (
        SELECT
            team_id,
            count(1) AS team_size
        FROM Employee
        GROUP BY team_id
    ) AS t
        ON e.team_id = t.team_id;

解法 2:

# Write your MySQL query statement below
SELECT
    e1.employee_id,
    count(*) AS team_size
FROM
    Employee AS e1
    LEFT JOIN Employee AS e2 ON e1.team_id = e2.team_id
GROUP BY e1.employee_id;