Mysql 如何解决leetcodesql使用分区查找团队规模的问题?

Mysql 如何解决leetcodesql使用分区查找团队规模的问题?,mysql,sql,Mysql,Sql,这是Leetcode网站上的问题 我的问题是: Employee Table: +-------------+------------+ | employee_id | team_id | +-------------+------------+ | 1 | 8 | | 2 | 8 | | 3 | 8 | | 4 | 7 | | 5

这是Leetcode网站上的问题

我的问题是:

Employee Table:
+-------------+------------+
| employee_id | team_id    |
+-------------+------------+
|     1       |     8      |
|     2       |     8      |
|     3       |     8      |
|     4       |     7      |
|     5       |     9      |
|     6       |     9      |
+-------------+------------+
Result table:
+-------------+------------+
| employee_id | team_size  |
+-------------+------------+
|     1       |     3      |
|     2       |     3      |
|     3       |     3      |
|     4       |     1      |
|     5       |     2      |
|     6       |     2      |
+-------------+------------+
Employees with Id 1,2,3 are part of a team with team_id = 8.
Employees with Id 4 is part of a team with team_id = 7.
Employees with Id 5,6 are part of a team with team_id = 9.

我的查询有什么问题,或者如何使用分区来解决此问题?

使用
计数作为分析函数:

select employee_id, count(employee_id) OVER(PARTITION BY team_id) team_size 
from employee

你的问题看起来不错。有什么问题?@GMB刚刚注意到,
:~
也许他们想要的是
COUNT(*)
而不是
COUNT(员工id)
,但结果其实是一样的。也许Leetcode不识别窗口函数。
SELECT
    employee_id,
    team_id,
    COUNT(*) OVER (PARTITION BY team_id) AS team_size
FROM Employee
ORDER BY employee_id;