tags:

views:

72

answers:

1

I am trying to write a statement for counting the employees attendance and execute their id , name and the days that he has working on the last 3 months by counting the duplicate id on NewTimeAttendance for month 1, 2 and 3.

I tried to count:

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

This is absolutely working, but just for one employee.

The second try:

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

This is working, but it counts all employees and I want it to execute each EmployeeId, EmployeeName and number of days as new record.

Last try: (before you see the code ... it is wrong ..but I am trying)

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

I really need help...thanks in advance.

+5  A: 

You need a GROUP BY function, try:

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
Philip Fourie
You're going to need to put EmployeeName in your groupBy statement as well, I think.
Paddy
@Paddy, thanks you are correct. I have updated my answer accordingly.
Philip Fourie
i'll see.........!
jjj
fantastic...simple ...and amazing ..thanks...
jjj
@Paddy...thanks
jjj