2011-04-15 48 views
0

我有分配任务的工作人员,我需要查找一名工作人员年初完成的分配给他的任务的百分比。如果约翰分配了10个任务,并且完成了5个,我需要显示John已经关闭了.50(50%)。查找人员关闭比率

我有两个表: 任务和Tasks_cstm

任务tl

| ID | STATUS |Date_Completed| 

的状态是 '进行中', '未启动', '已完成'

Tasks_cst TC

| ID_C|Staff_Member| 

表格已加入t.id = tc.id_c

这将返回完成数:

(select count(*)as Completed from tasks_CSTM tc 
join tasks t 
on t.id = tc.id_c 
where status = 'completed' 
group by staff_member_C) 

这将返回任务总数:

(select count(*)as Total from tasks_CSTM tc 
join tasks t 
on t.id = tc.id_c 
group by staff_member_C) 

这是我想出,但它的错误:子查询返回多个比1值。

select staff_member_c,((select count(*)as Completed from tasks_CSTM tc 
join tasks t 
on t.id = tc.id_c 
where status = 'completed' 
group by staff_member_C)/(select count(*)as Total from tasks_CSTM tc 
join tasks t 
on t.id = tc.id_c 
group by staff_member_C)) 
from tasks t 
join tasks_CSTM tc 
on t.id = tc.id_C 
group by staff_member_C 

任何帮助表示赞赏。

回答

0

这里有几个问题需要解决。其中之一是处理“年初至今”的部分。现在,使用Date_Completed列,无法知道任务何时分配/创建,这会使我们无法了解年初至今的信息。除了这个问题的一部分,这是我应该工作的查询。我有一个WHERE子句注释掉了,它可以很容易地修改为使用Date_Assigned列作为年初至今的信息。

select 
    staff_member 
    , sum(case t.status when 'Completed' then 1.0 else 0 end) [Completed] 
    , count(*) [Total] 
    , sum(case t.status when 'Completed' then 1.0 else 0 end)/count(*) [CompletedPercent] 
from 
    tasks t 
inner join tasks_cstm tc 
    on t.id = tc.id_C 
--where 
-- dateadd(year, datediff(year, 0, Date_Assigned), 0) = dateadd(year, datediff(year, 0, getdate()), 0) 
group by 
    staff_member 

而这里的设置代码我用(UN-通俗易懂的)测试一下:

create table tasks (ID int, Status varchar(50), Date_Completed date) 
create table tasks_cstm (ID_C int, Staff_Member varchar(50)) 

insert into tasks 
select 1, 'Not Started', null 
union all 
select 2, 'Completed', '2011-04-15' 
union all 
select 3, 'In Progress', null 

insert into tasks_cstm 
select 1, 'Cadaeic' 
union all 
select 2, 'Cadaeic' 
union all 
select 3, 'Cadaeic' 

在由此产生:

staff_member  Completed   Total  CompletedPercent 
------------------- -------------------- ----------- ----------------------- 
Cadaeic    1.0     3   0.333333 
+0

非常感谢,感谢您的帮助。 – Stan 2011-04-15 21:50:16

1

事情是这样的,我认为:

select staff_member_c, sum(case when status='completed' then 1.0 end)/count(*) as pctCompleted 
from tasks_cstm tc 
join tasks t 
on t.id = tc.id_c 
group by staff_member_c 

您可能需要 “其他0.0” 的情况说明(但不要在MSSQL),你可能需要NULLIF(COUNT(*),0)在分母中(但可能不在任何DBMS中)。

0
-- Tasks 
declare @T table(ID int, Status varchar(20)) 
-- Tasks_cst 
declare @TC table(ID_C int, Staff_Member varchar(20)) 

insert into @TC values (1, 'Staff 1') 
insert into @TC values (2, 'Staff 2') 
insert into @TC values (3, 'Staff 3') 

insert into @T values (1, 'Completed') 
insert into @T values (1, 'Completed') 
insert into @T values (1, 'In Progress') 
insert into @T values (2, 'Completed') 
insert into @T values (2, 'In Progress') 
insert into @T values (3, 'In Progress') 

select * 
from @TC as TC 
    cross apply 
    (select sum(case T.Status when 'Completed' then 1.0 else 0.0 end)/count(*) 
    from @T as T 
    where T.ID = TC.ID_C) as C(PrecentCompleted) 

结果

ID_C  Staff_Member   PrecentCompleted 
----------- -------------------- --------------------------------------- 
1   Staff 1    0.666666 
2   Staff 2    0.500000 
3   Staff 3    0.000000