SELECT DISTINCT
F_Emp_code,CONVERT(varchar, t.F_Log_dtPunched, 103) AS F_Tbl_dtpunched,
Loc.F_Loc_code,COUNT(TF.F_Ter_LocCode)'Worked'
FROM
(select *, row_number() over (partition by F_Emp_code order by F_Emp_code) as rn from T_Mst_Employee ) A
LEFT JOIN T_Mst_Company co ON co.F_Com_code = A.F_Emp_Company_ID
RIGHT JOIN T_Tra_Loginfo t ON t.F_Log_Emp_Code = F_Emp_code
JOIN dbo.T_Mst_terminalinfo TF ON TF.F_Ter_nTerminalID=t.F_Log_TerminalID
LEFT JOIN T_Mst_Location Loc ON Loc.F_Loc_code = TF.F_Ter_LocCode
WHERE A.F_Emp_Status = 'Active'
AND F_Emp_code='100229'
and t.F_Log_dtPunched between '2017-01-01' and '2017-01-08'
group by co.F_Com_Logo, A.f_emp_firstname,A.F_Emp_MiddleName,
A.F_Emp_LastName,F_Emp_code,
F_Log_dtPunched,co.F_Com_Desc,Loc.F_Loc_code,Loc.F_Loc_Desc,TF.F_Ter_nTerminalID
My result is like this :
empid date loccode worktime
100229 01/01/2017 05138 1
100229 02/01/2017 05138 1
100229 03/01/2017 05138 1
100229 05/01/2017 09409 1
100229 06/01/2017 05138 1
100229 07/01/2017 01305 1
100229 07/01/2017 05138 1
如果任何员工使用不同的位置工作相同的日期,那么我想在工作时间显示结果= 1 /不同位置的计数
例如,如果员工在同一天工作了3个位置,那么我需要计算工作时间列1/3的工作时间
答案 0 :(得分:0)
您正在寻找COUNT(DISTINCT column) OVER (...)
:
worktime * 1.0 / count(distinct loccode) over (partition by emp_id, date)
为了避免整数除法,必须乘以1.0。