在单个sql中按部门获取重复的员工计数

在单个sql中按部门获取重复的员工计数,sql,Sql,我想你想要这样一个SQL ID Name dep_id 1 A 1 2 B 2 3 A 1 4 A 2 5 B 2 6 A 2 由于上次编辑(您想添加到这个答案),请考虑由DepTyIDID:分组 请详细解释您想要实现的目标,并在问题中添加预期结果。为什么您要回答这样

我想你想要这样一个SQL

ID       Name        dep_id
1        A           1
2        B           2
3        A           1
4        A           2
5        B           2
6        A           2

由于上次编辑(您想添加到这个答案),请考虑由DepTyIDID:

分组
请详细解释您想要实现的目标,并在问题中添加预期结果。为什么您要回答这样一个低质量的问题?这是你在这里问问题的标准吗?
with tab( ID, Name, dep_id) as
(
 select 1,'A',1 union all
 select 2,'B',2 union all 
 select 3,'A',1 union all
 select 4,'A',2 union all
 select 5,'B',2 union all
 select 6,'A',2 
)
select name, 
       count(dep_id) as dept_count 
  from tab t
 group by name
 having count(name)>1;

NAME  DEPT_COUNT
----  ----------
 A        4
 B        2
with tab( ID, Name, dep_id) as
(
 select 1,'A',1 union all
 select 2,'B',2 union all 
 select 3,'A',1 union all
 select 4,'A',2 union all
 select 5,'B',2 union all
 select 6,'A',2 
)
select name, dept_id, 
       count(dept_id) as dept_count 
  from tab t
 group by name, dept_id
 having count(name)>1;

 NAME   DEPT_ID DEPT_COUNT
 ----   ------  ----------
  A        2         2
  A        1         2
  B        2         2