My goal is to produce the following table:
| EmployeeName | Status1 | Status2 | Status3 |
| Employee A | CountA1 | CountA2 | CountA3 |
| Employee B | CountB1 | CountB2 | CountB3 |
| Employee C | CountC1 | CountC2 | CountC3 |
| Total | Total1 | Total2 | Total3 |
My issues are:
- The script takes a couple minutes to run.
- It seems clunky the way I have approached by creating a temporary table
Here's my script:
Drop Table NewProjectTable
select
EM.Org,Organization.Name as Office,coalesce(em.LastName,'')+', '+coalesce(em.PreferredName,'') as Employee,
(select count(*) from ProjectTable where ProjNbr in (select ProjNbr from ProjectTable where ProjMgr=em.employee) and Status='Status1') as Status1,
(select count(*) from ProjectTable where ProjNbr in (select ProjNbr from ProjectTable where ProjMgr=em.employee) and Status='Status2') as Status2,
(select count(*) from ProjectTable where ProjNbr in (select ProjNbr from ProjectTable where ProjMgr=em.employee) and Status='Status3') as Status3,
into NewProjectTable
from
em,organization
where em.org = organization.org
and em.status = 'A'
declare @Status1Total int
declare @Status2Total int
declare @Status3Total int
select @Status1Total = count(*) from ProjectTable where Status='Status1'
select @Status2Total = count(*) from ProjectTable where Status='Status2'
select @Status3Total = count(*) from ProjectTable where Status='Status3'
insert into NewProjectTable
(Org,Office,Employee,Status1,Status2,Status3)
values (99,'Total',space(5),@Status1Total,@Status2Total,@Status3Total)