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
;
相关推荐
冬奇Lab21 小时前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence1 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神1 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据2 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_2 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡2 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧2 天前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon2 天前
SQL学习指南——视图
数据库·sql
活宝小娜2 天前
mysql详细安装教程
数据库·mysql·adb
贤时间2 天前
codex 助力oracle ebs 开发
数据库·oracle