2017-04-06 53 views
1

員工每週參與多達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 
+0

請閱讀https://stackoverflow.com/help/mcve的UNION。 – jarlh

+0

爲什麼你查詢'WHERE ProjectID1不爲null,Project1TaskID1爲空'且'WHERE ProjectID1不爲null,Project1TaskID1不爲空。 「WHERE ProjectID1不是空值的查詢」是否涵蓋該場景中的所有情況? – Jens

+0

我想你在你的查詢中重複了一些union ...此外,如果你發佈了一些示例數據(CREATE和INSERT腳本) – etsa

回答

0

這是你在找什麼?

我加入COUNT(*) OVER (PARTITION BY SAGEID, A.EmployeeID) AS RC 和修改的所有吉恩也建議

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 
+0

伯爵已經發現了,正是我以前的樣子,我明白你對工會的所有意思,我可以刪除一個,但不是你建議的那個有人可以在一個項目上工作,整個星期只執行一種任務,或者有人可以在一個項目上工作,執行兩種任務,但他們不能在一個項目上工作,也不能完成任務 – hero9989

+0

我糾正了UNION正如延斯所建議的 – etsa