微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何计算团队和成员

如何解决如何计算团队和成员

我有一张这样的桌子:

| 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 ;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。