サブクエリを使用してこれを実行できます。
SELECT
lRecordID, lEmployeeID, dtTimeIn, dtTimeOut, sEnteredBy, sUpdatedBy,
dtUpdated, sDept, fHoursWorked, lDeptID, dblHourlyWage, fRegHours, dblRegLabor,
fOTHours, dblOTLabor, dtTimePunchIn, dtTimePunchOut, fPunchedHours
FROM TimeClock c
LEFT JOIN
(
select MIN(dtTimeIn) MindtTimeIn, CAST(dtTimeIn as DATE) dt
from timeclock
group by CAST(dtTimeIn as DATE)
) MinEmp
on c.dtTimeIn = MinEmp.MindtTimeIn
and CAST(c.dtTimeIn as DATE) = MinEmp.dt
LEFT JOIN
(
select MAX(dtTimeOut) MaxdtTimeOut, CAST(dtTimeOut as DATE) dt
from timeclock
group by CAST(dtTimeOut as DATE)
) MaxEmp
on c.dtTimeOut = MaxEmp.MaxdtTimeOut
and CAST(c.dtTimeOut as DATE) = MaxEmp.dt
WHERE (dtTimeIn > @startdate)
AND (dtTimeOut < @enddate)
AND (sDept IN ('1', '2', '3'))
SQL Server 2008+ を使用していない場合は、DATE
データ型がないため、次を使用できます。
SELECT
lRecordID, lEmployeeID, dtTimeIn, dtTimeOut, sEnteredBy, sUpdatedBy,
dtUpdated, sDept, fHoursWorked, lDeptID, dblHourlyWage, fRegHours, dblRegLabor,
fOTHours, dblOTLabor, dtTimePunchIn, dtTimePunchOut, fPunchedHours
FROM TimeClock c
LEFT JOIN
(
select MIN(dtTimeIn) MindtTimeIn, Convert(char(10), dtTimeIn, 120) dt
from timeclock
group by Convert(char(10), dtTimeIn, 120)
) MinEmp
on c.dtTimeIn = MinEmp.MindtTimeIn
and Convert(char(10), c.dtTimeIn, 120) = MinEmp.dt
LEFT JOIN
(
select MAX(dtTimeOut) MaxdtTimeOut, Convert(char(10), dtTimeOut, 120) dt
from timeclock
group by Convert(char(10), dtTimeOut, 120
) MaxEmp
on c.dtTimeOut = MaxEmp.MaxdtTimeOut
and Convert(char(10), c.dtTimeOut, 120 = MaxEmp.dt
WHERE (dtTimeIn > @startdate)
AND (dtTimeOut < @enddate)
AND (sDept IN ('1', '2', '3'))