我有分配任务的工作人员,我需要找到工作人员年初至今完成的任务的百分比......分配给他的那些任务。如果约翰被分配了10个任务,并且完成了5个我需要表明约翰已经关闭了.50(50%)。
我有两张桌子: 任务和任务_cstm
任务t
| 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 :(得分: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
你可能在case语句中需要“else 0.0”(但不要在MSSQL中),并且你可能需要在分母中使用nullif(count(*),0)(但可能不在任何DBMS中)。
答案 1 :(得分: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
以下是我曾经(不可理解地)测试它的设置代码:
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
答案 2 :(得分: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