如何计算团队和成员

问题描述

我有一张这样的桌子:

| personid |主管| date_in |

每个人只能有一个主管(他本人也可以有一个主管)。

我想计算:

  • “团队”的数量,即没有监督者且至少有两个以下人员的人数;
  • 每个团队的成员人数;
  • “活跃团队”的数量定义为少于X天前新增的团队。

预先感谢您的帮助。

数据:

personid|supervisorid   |datein
--------+---------------+----------
001     |NA             |01/09/2020
002     |001            |01/09/2020
003     |001            |01/09/2020
004     |003            |01/09/2020
005     |003            |01/09/2020
006     |003            |01/09/2020
007     |003            |01/09/2020
008     |NA             |01/09/2020
009     |008            |01/01/1990
010     |008            |01/01/1990
011     |NA             |01/01/1990
012     |011            |01/01/1990

结果:

-number of teams:2
-members per team:

supervisor team|num_members
---------------+-----------
001            |7
008            |3

-active teams in the last 30 days: 1 (supervisorid=001)

解决方法

如果您有sas,则可以使用proc sql *“团队”的数量应为不具有主管且至少有两名以下人员的人员数量;

select count(distinct a.personid) as teams 
from (select personid from yourtable where supervisorid='NA' group by 1) a   
inner join (select supervisorid,count(distinct personid) as num_members from yourtable group by 1) b on a.personid=b.supervisorid ;

*每个团队的成员人数;

select a.person_id as supervisorteam,b.num_members
from(select personid from yourtable where supervisorid='NA' group by 1) a   
inner join (select supervisorid,count(distinct personid) as num_members from yourtable group by 1 having num_members>1) b on a.personid=b.supervisorid ;

*“活跃团队”的数量定义为少于X天前添加新人员的团队

select count(distinct a.personid) as active
from (select personid from yourtable where supervisorid='NA' and datein<Xdaysago group by 1) a   
inner join (select supervisorid,count(distinct personid) as num_members from yourtable group by 1 having num_members>1) b on a.personid=b.supervisorid ;