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
;
相关推荐
Live&&learn17 小时前
算法训练-数据结构
数据结构·算法·leetcode
w***42418 小时前
【MySQL】复合查询
数据库·mysql
q***017719 小时前
【MySQL】表的基本操作
数据库·mysql·oracle
电子_咸鱼19 小时前
【STL string 全解析:接口详解、测试实战与模拟实现】
开发语言·c++·vscode·python·算法·leetcode
budingxiaomoli19 小时前
存储过程和触发器
数据库
q***125319 小时前
PostgreSQL_安装部署
数据库·postgresql
q***482519 小时前
mysql用户名怎么看
数据库·mysql
夏日玲子19 小时前
【Redis】Redis常用命令合集
数据库·redis·缓存
万邦科技Lafite20 小时前
1688图片搜索商品API接口(item_search_img)使用指南
java·前端·数据库·开放api·电商开放平台
自在极意功。20 小时前
SQL查询语句深度解析:从基础到进阶,写出高效优雅的SQL!
数据库·sql