【问题标题】:How to count teams and members如何计算团队和成员
【发布时间】:2020-09-24 10:14:50
【问题描述】:

我有一张这样的桌子:

|人物|监督者|日期输入|

每个人只能有一个主管(即一个人自己可以有一个主管)。

我想计算:

  • “团队”的数量应该是没有主管且其下至少有两个人的人数;
  • 每个团队的人数;
  • “活跃团队”的数量定义为在 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)

【问题讨论】:

  • 请提供样本数据和期望的结果。它会澄清你想要做什么。
  • 不应该personid = 011也有一个团队,由1名成员组成?
  • 团队是一组人员,其主管有 >= 2 人(如所述)
  • 你还应该展示你试图解决这个问题的方法。为什么要限制 SQL?

标签: sql tree sas data-mining hierarchical-data


【解决方案1】:

如果你有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 ;

【讨论】:

  • 感谢您的回复,但不太正确。团队可以是“多级”的,因此主管 001 的团队有 7 名成员,因为:级别 0:001;级别 1 : 002, 003 级别 2 : 004, 005, 006, 007(因为它们都由 003 监督)。根据您发布的代码,它只有 3 人
猜你喜欢
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多