員工每週參與多達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表當前值從上述查詢 電流輸出
我要補充 - 所期望的輸出是相同的所述第二圖像(以上)但帶有稱爲LineCount的額外列
LineCount
5
5
5
5
5
2
2
1
2
2
請閱讀https://stackoverflow.com/help/mcve的UNION。 – jarlh
爲什麼你查詢'WHERE ProjectID1不爲null,Project1TaskID1爲空'且'WHERE ProjectID1不爲null,Project1TaskID1不爲空。 「WHERE ProjectID1不是空值的查詢」是否涵蓋該場景中的所有情況? – Jens
我想你在你的查詢中重複了一些union ...此外,如果你發佈了一些示例數據(CREATE和INSERT腳本) – etsa