Hi Ramesh,
This example might be more complex than what you need, but it should give you a starting point. Caveat - This example
only counts the time that fall within @start and @end period. So for example if an employee started work at 11/1/06 6:30AM, and you entered a @start date of 11/1/06 7:30 am, the hour between 6:30AM and 7:30AM would
not be counted.
--- counts ONLY the time worked between @start and @end
SELECT e.emp_id,
SUM
(
DATEDIFF
(n,
CASE WHEN login_time < @start THEN @start ELSE login_time END,
CASE WHEN logout_time > @end THEN @end ELSE logout_time END
)
)
AS TotalTimeWithinDateRange
FROM LoginTable lt
INNER JOIN Store s ON lt.store_id = s.store_id
INNER JOIN Employee e ON lt.emp_id = e.emp_id
WHERE s.store_id = @store_id AND
(
(lt.login_time BETWEEN @start AND @end) OR
(lt.logout_time BETWEEN @start AND @end) OR
(lt.login_time <= @start and logout_time >= @end)
)
GROUP BY e.emp_id