我有一个包含多项任务的项目。人们处理这些任务,我想从下表中获得“自治因素/人”。该表说明了由谁在每项任务中采取的行动:
+-------+-------------+------------------+-----------+--------------+ | Task# | Action Type | Time | Action By | Completed By | +-------+-------------+------------------+-----------+--------------+ | 1 | Assigment | 2015-07-23 10:00 | Nick | Nick | | 1 | Resolution | 2015-07-23 10:40 | Nick | Nick | | 1 | Closure | 2015-07-23 12:41 | Nick | Nick | +-------+-------------+------------------+-----------+--------------+ | 2 | Assigment | 2015-07-24 10:00 | Nick | Nick | | 2 | Assigment | 2015-07-24 10:30 | John | Nick | | 2 | Resolution | 2015-07-24 11:30 | Nick | Nick | | 2 | Closure | 2015-07-24 12:45 | Nick | Nick | +-------+-------------+------------------+-----------+--------------+ | 3 | Assigment | 2015-07-25 10:00 | John | Nick | | 3 | Resolution | 2015-07-25 11:00 | Nick | Nick | | 3 | Closure | 2015-07-25 13:50 | Nick | Nick | +-------+-------------+------------------+-----------+--------------+ | 4 | Assigment | 2015-07-26 10:00 | Nick | Nick | | 4 | Assigment | 2015-07-26 10:30 | John | Nick | | 4 | Resolution | 2015-07-26 10:40 | Nick | Nick | | 4 | Assigment | 2015-07-26 11:50 | John | Nick | | 4 | Closure | 2015-07-26 14:00 | Nick | Nick | +-------+-------------+------------------+-----------+--------------+
当一个人来到他身边时,他被认为是自主的,他完成并从第一次关闭。
例如:
因此,尼克自主速度提高了50%(他完成并关闭了4个任务,但他只有2个自主关闭)。约翰自治因子是0%(他没有完成任何任务)。
总之,尼克认为自治,如果: 尼克是关闭任务的人,没有人在他面前触摸它(例如:任务#1), 2.或者如果有多个人在尼克之前完成任务但不包括尼克,他是最后一个并且他关闭了它(如任务#3,但不像任务#2,4)。
所以问题是,是否有可能在上表中运行的SQL查询或简单代码可以获得自治因子/人,即结果预期如下:
+------+-------------------+
| Name | Autonomous Factor |
+------+-------------------+
| Nick | 50% |
| John | 0% |
+------+-------------------+
答案 0 :(得分:1)
这听起来像是聚合的聚合。我认为“自主”逻辑只是在有人关闭它和第一次看到任务之间没有人触及任务。这可以通过以下规则表达:
要收集此信息,您需要考虑分配给该任务的每个人。这需要在任务人员和任务数据之间cross join
。
结果查询:
select t.task, p.person,
(case when min(t.actionby) = max(t.actionby) then 1
when (max(case when t.actionby <> p.person then time end) >
min(case when t.actionby = p.person then time end)
) and
(max(case when t.actionby = p.person then time end) =
max(case when t.action = 'Closure' then time end)
)
then 1 else 0
end) as IsAutonomous
from (select distinct task, actionby as person from tasks) p join
tasks t
on p.task = t.task
group by t.task, p.person;
获得此信息后,剩下的只是一个额外的聚合:
select person, avg(IsAutonomous)
from (select t.task, p.person,
(case when min(t.actionby) = max(t.actionby) then 1
(max(case when t.actionby <> p.person then time end) >
min(case when t.actionby = p.person then time end)
) and
(max(case when t.actionby = p.person then time end) =
max(case when t.action = 'Closure' then time end)
)
then 1 else 0
end) as IsAutonomous
from (select distinct task, actionby as person from tasks) p join
tasks t
on p.task = t.task
group by t.task, p.person
) tp
group by person;