select convert(varchar(10), TS_Day,101) as Date, EmployeeID, Name,
convert(varchar,datediff(hh,0,(dateadd(ss,sum(ts_seconds) ,0))))+
substring((convert(varchar(8),(dateadd(ss,sum(ts_seconds) ,0)),114)),3,3) as HrsMin
from
(
SELECT TS_day, L.EmployeeId, L.LastName + ', ' + L.FirstName AS Name, Sum(TS_Seconds) as ts_Seconds
FROM
(
select company,employeeid,
convert(datetime,convert(varchar(8),[timeOut],112)) as TS_Day,
timein, [timeout], BreakFlag,
IsNull ( ( select top 1 case when t1.Breakflag = 1 then datediff(ss,t1.timeout, t2.Timein) else 0 end
from EmployeeHours t2
where t2.employeeid = t1.employeeid
and t2.company=t1.company
and convert(varchar(8),t2.timein) = convert(varchar(8),t1.timein) -- must be same day
order by t2.timein asc, t2.timeout asc, recordid asc),0) as ts_seconds
from EmployeeHours t1
) times
INNER JOIN EmployeeCustomFieldsData C ON (times.EmployeeId = C.EmployeeId) AND (times.Company = C.Company)
INNER JOIN EmployeeList L ON (C.EmployeeId = L.EmployeeId) AND (C.Company = L.Company)
LEFT JOIN UserList U ON L.ManagerUID = U.UID and L.Company = U.Company
WHERE convert(datetime,convert(char(8),times.TimeIn,112)) between @From and @To
AND L.Suspend = 0
AND C.CustomFieldData in ('DH', 'CL', 'EX')
AND times.Company=1
GROUP BY times.employeeid, L.EmployeeId, L.LastName + ', ' + L.FirstName,ts_Day
) daily
GROUP BY employeeid, name, ts_Day
HAVING (sum(ts_Seconds) / 60) >16
ORDER BY name, employeeid, ts_Day
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
by: mark_willsPosted on 2009-10-15 at 05:44:39ID: 25579897
This is complex, and vaguely familiar - have we been here before ?