2010-03-15 78 views
2

我想寫一份聲明中用於計算員工考勤和執行他們的idname,他一直工作在過去3個月的天爲1個月,2和3指望NewTimeAttendance重複id計數員工考勤

我試圖計算:

Select COUNT(employeeid) 
    from NewTimeAttendance 
where employeeid=1 and (month=1 or month = 2 or month = 3) 

這絕對是工作,但只是一名僱員。

的第二次嘗試:

SELECT COUNT(NewEmployee.EmployeeID) 
    FROM NewEmployee INNER JOIN NewTimeAttendance 
    ON NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
    and (month=1 or month =2 or month = 3) 

這是工作,但它計算所有的員工,我希望它執行的每個EmployeeIdEmployeeNamenumber of days作爲新的記錄。

最後一次嘗試:(你看到的代碼之前...這是錯的..但我想)

for i in 0..27 loop 
    SELECT COUNT(NewEmployee.EmployeeID),NewEmployee.EmployeeId,EmployeeName 
    FROM NewEmployee INNER JOIN NewTimeAttendance 
     ON NewEmployee.EmployeeID(i) = NewTimeAttendance.EmployeeID 
     and (month=1 or month =2 or month = 3) 
end loop 

我真的需要幫助...在此先感謝。

+0

@jjj,很高興見到你已經嘗試自己先解決這個問題。 – 2010-03-15 12:30:39

+0

^_^... ..感謝 – jjj 2010-03-15 12:48:03

回答

5

你需要一個GROUP BY功能,請嘗試:

SELECT COUNT(NewEmployee.EmployeeID), NewEmployee.EmployeeId,EmployeeName 
FROM NewEmployee INNER JOIN NewTimeAttendance 
    ON NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
    and (month=1 or month =2 or month = 3) 
GROUP BY NewEmployee.EmployeeID, EmployeeName 
+1

我想你也需要將EmployeeName放入你的groupBy語句中。 – Paddy 2010-03-15 12:32:25

+1

@ Paddy,謝謝你是對的。我已經相應地更新了我的答案。 – 2010-03-15 12:47:07

+0

我會看到.........! – jjj 2010-03-15 12:52:25