开发者

Find a persons closing ratio

I have staff members that are assigned tasks, I need to find the percentage of tasks that a staff member has completed year-to-date... of those that were assigned to him. If John is assigned 10 tasks, and completed 5 I need to show John has closed .50 (50%).

I have two tables: Tasks and Tasks_cstm

Tasks t

| ID | STATUS |Date_Completed|

The statuses are 'In Progress', 'Not Started', 'Completed'

Tasks_cst tc

| ID_C|Staff_Member|

The tables are joined on t.id = tc.id_c

This returns the number completed:

(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 )

This returns the total number of tasks:

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

This is what I've come up with, but it errors: Subquery returned more than 1 value.

select staff_member_c,((select  count(*)as Completed from tasks_CSTM tc
开发者_如何学JAVAjoin 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

Any help is appreciated.


Something like this I think:

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

You might need "else 0.0" in the case statement (but don't in MSSQL), and you might need nullif(count(*),0) in the denominator (but probably not in any DBMS).


There's a couple issues here to grapple with. One of which is dealing with the "year-to-date" part. Right now, with a Date_Completed column, there's no way to know when a task was assigned/created, which invalidates our ability to know year-to-date info. Barring that part of the question, here's my query which should work. I have a WHERE clause commented out which can easily be adapted to use a Date_Assigned column for year-to-date info.

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

And here's the setup code I used to (un-comprehensibly) test it out:

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'

Resulting in this:

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


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

Result

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜