leetcode 1303 求团队人数(postgresql)

需求

员工表:Employee

±--------------±--------+

| Column Name | Type |

±--------------±--------+

| employee_id | int |

| team_id | int |

±--------------±--------+

employee_id 字段是这张表的主键,表中的每一行都包含每个员工的 ID 和他们所属的团队。

编写一个 SQL 查询,以求得每个员工所在团队的总人数。

查询结果中的顺序无特定要求。

查询结果格式示例如下:

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 |

±------------±-----------+

ID 为 1、2、3 的员工是 team_id 为 8 的团队的成员,

ID 为 4 的员工是 team_id 为 7 的团队的成员,

ID 为 5、6 的员工是 team_id 为 9 的团队的成员。

输入

输出

sql 复制代码
with t1 as (
select team_id,count(1) as num
from employee
group by team_id
)
select e.employee_id,num
from employee e,t1
where e.team_id=t1.team_id
;
相关推荐
byoass14 分钟前
企业云盘与设计软件深度集成:AutoCAD/Revit/SolidWorks插件开发与API集成实战
服务器·网络·数据库·安全·oracle·云计算
样例过了就是过了42 分钟前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
爬山算法1 小时前
MongoDB(113)如何使用第三方工具进行MongoDB监控?
数据库·mongodb
Navigator_Z2 小时前
LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays
c语言·算法·leetcode
早日退休!!!2 小时前
大模型推理瓶颈七层分析模型
java·服务器·数据库
大山同学2 小时前
claudecode精炼版-CoreCoder
数据库·人工智能·claude code·corecoder
of Watermelon League2 小时前
5、使用 pgAdmin4 图形化创建和管理 PostgreSQL 数据库
数据库·postgresql
Dontla3 小时前
Python asyncpg库介绍(基于Python asyncio的PostgreSQL数据库驱动)连接池、SQLAlchemy
数据库·python·postgresql
俺不要写代码3 小时前
数据库:DQL
数据库·sql·mysql
zh1570233 小时前
如何编写动态SQL存储过程_使用sp_executesql执行灵活查询
jvm·数据库·python