An employee is involved in up to 3 projects in a week and in each of those projects, the employee can perform 2 different tasks. I'm using union all to join separate fields from a single table so I get a separate row for each task performed (up to 6 rows per employee). This will eventually join to the clock in system so the amount we pay each employee can be assigned to tasks on certain projects so we can better track where money is going.
I need a means to count the amount of tasks (so counting the number of rows for each employee) so I have a number to divide by.
I've tried a group by and count in several locations but it just shows 1 (presumably because there is no unique field within the selection (each row is unique but that doesn't seem to help?)).
Should I be doing the count on a specific field? or is there another way?
Current SQL Query
DECLARE @WeekCommencing date = '2017-04-03'
select SageID, a.EmployeeID, a.ProjectID, a.TaskID, a.FlatRate, a.PayRate from
( Select distinct Employee.SageID, EmployeeProject.EmployeeID, EmployeeProject.ProjectID, EmployeeProject.TaskID, Employee.FlatRate, Employee.PayRate from
( Select EmployeeID, ProjectID1 as ProjectID, Project1TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID1 is not null and Project1TaskID1 is null
union all
Select EmployeeID, ProjectID1 as ProjectID, Project1TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID1 is not null and Project1TaskID1 is not null
union all
Select EmployeeID, ProjectID1 as ProjectID, Project1TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID1 is not null and Project1TaskID2 is not null
union all
Select EmployeeID, ProjectID2 as ProjectID, Project2TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID2 is not null and Project2TaskID1 is null
union all
Select EmployeeID, ProjectID2 as ProjectID, Project2TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID2 is not null and Project2TaskID1 is not null
union all
Select EmployeeID, ProjectID2 as ProjectID, Project2TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID2 is not null and Project2TaskID2 is not null
union all
Select EmployeeID, ProjectID3 as ProjectID, Project3TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID3 is not null and Project3TaskID1 is null
union all
Select EmployeeID, ProjectID3 as ProjectID, Project3TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID3 is not null and Project3TaskID1 is not null
union all
Select EmployeeID, ProjectID3 as ProjectID, Project3TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID3 is not null and Project3TaskID2 is not null
) as EmployeeProject
inner join
Employee on EmployeeProject.EmployeeID = Employee.EmployeeID
where WeekCommencing = @WeekCommencing
) as a
inner join
( Select ProjectEmployeeID, EmployeeID from -- remove duplicates if info is inserted multiple times
( Select ProjectEmployeeID, EmployeeID, ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY ProjectEmployeeID DESC) EmployeeDup
FROM ProjectEmployee
) a
WHERE EmployeeDup = 1
) as c
on a.EmployeeID = c.EmployeeID
Order by a.flatrate, a.SageID
Current Values in the ProjectEmployee table Current Output from above query
I should add - the desired output is the same as the second image (above) but with an extra column called LineCount
LineCount
5
5
5
5
5
2
2
1
2
2
Is this what you are looking for?
I added COUNT(*) OVER (PARTITION BY SAGEID, A.EmployeeID) AS RC
and modified the UNION ALL as Jean too suggested
DECLARE @WeekCommencing date = '2017-04-03'
select SageID, a.EmployeeID, a.ProjectID, a.TaskID, a.FlatRate, a.PayRate, COUNT(*) OVER (PARTITION BY SAGEID, A.EmployeeID) AS RC
from ( Select distinct Employee.SageID, EmployeeProject.EmployeeID, EmployeeProject.ProjectID, EmployeeProject.TaskID, Employee.FlatRate, Employee.PayRate
from (
Select EmployeeID, ProjectID1 as ProjectID, Project1TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID1 is not null
union all
Select EmployeeID, ProjectID1 as ProjectID, Project1TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID1 is not null and Project1TaskID2 is not null
union all
Select EmployeeID, ProjectID2 as ProjectID, Project2TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID2 is not null
union all
Select EmployeeID, ProjectID2 as ProjectID, Project2TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID2 is not null and Project2TaskID2 is not null
union all
Select EmployeeID, ProjectID3 as ProjectID, Project3TaskID1 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID3 is not null
union all
Select EmployeeID, ProjectID3 as ProjectID, Project3TaskID2 as TaskID, WeekCommencing
FROM ProjectEmployee
WHERE ProjectID3 is not null and Project3TaskID2 is not null
) AS EMPLOYEEPROJECT
INNER JOIN EMPLOYEE ON EMPLOYEEPROJECT.EMPLOYEEID = EMPLOYEE.EMPLOYEEID
WHERE WeekCommencing = @WeekCommencing
) AS A
INNER JOIN (SELECT ProjectEmployeeID, EmployeeID
from -- remove duplicates if info is inserted multiple times
( Select ProjectEmployeeID, EmployeeID, ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY ProjectEmployeeID DESC) EmployeeDup
FROM ProjectEmployee
) a
WHERE EmployeeDup = 1
) AS C ON A.EMPLOYEEID = C.EMPLOYEEID
Order by a.flatrate, a.SageID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.