与联盟一起计算

时间:2017-04-06 08:56:04

标签: sql sql-server sql-server-2008

员工每周最多参与3个项目,在每个项目中,员工可以执行2个不同的任务。 我使用union all来连接单个表中的单独字段,因此我为每个执行的任务获得了一个单独的行(每个员工最多6行)。这最终将加入系统中的时钟,因此我们为每位员工支付的金额可以分配给某些项目的任务,这样我们就可以更好地跟踪资金流向。

我需要一种方法来计算任务量(所以计算每个员工的行数),所以我有一个数字除以。

我尝试了一个小组并在几个地方进行计数,但它只显示1(大概是因为选择中没有唯一的字段(每行都是唯一的,但似乎没有帮助?) )。

我应该对特定领域进行统计吗?还是有另一种方式?

当前SQL查询

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

ProjectEmployee表中的当前值 Current Values in the ProjectEmployee table 上述查询的当前输出 Current Output from above query

我应该添加 - 所需的输出与第二个图像(上图)相同,但是有一个名为LineCount的额外列

LineCount
5
5
5
5
5
2
2
1
2
2

1 个答案:

答案 0 :(得分:0)

这是你在找什么?

我添加了COUNT(*) OVER (PARTITION BY SAGEID, A.EmployeeID) AS RC 并修改了UNION ALL,因为Jean也建议

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