Good day
I have a sql server table EmpAttendence in which there are two fileds:
EmployeeID , Attendence_Date
1111 , 2011-11-11 07:00:00.000
1111 , 2011-11-11 08:00:00.000
1111 , 2011-11-11 09:00:00.000
1111 , 2011-11-11 11:00:00.000
1111 , 2011-11-11 12:00:00.000
1111 , 2011-11-11 16:00:00.000
2222 , 2011-11-11 08:00:00.000
whenever employee punch or insert his time card in the time clock device, a new record is added to that table so that in one day he might have many records.
I need to write sql statement that show me for each day for each employee how many hours that employee has worked
No the Employee
1111 has worked 7 hours
followed by other employees
i tried the following
but giving me wrong result
CREATE TABLE #datetab
(
employeeid INT,
attendence_date DATETIME
)
INSERT INTO #datetab
select FKemapEmID, CONVERT(DATETIME, CONVERT(CHAR(8), [apDate], 112)
+ ' ' + CONVERT(CHAR(8), [apTime], 108))
FROM [txnAcessPunches].[dbo].[atransAddPunch];
SELECT employeeid AS EMPID,
strdate AS [Day],
time_in,
time_out,
Datediff(HH,time_in,time_out) as 'TotalHours'
FROM (SELECT employeeid,
CONVERT(VARCHAR(10), attendence_date, 101) AS strDate,
Min(attendence_date) AS Time_in,
Max(attendence_date) AS Time_out
FROM #datetab
GROUP BY employeeid
,CONVERT(VARCHAR(10), attendence_date, 101)) t
ORDER BY employeeid,
time_in
drop table #datetab