Reputation: 37
I have a table for payslips which can contain multiple payslips with the same employee. Each payslip has a hours_worked column. I want to be able to get each employee's hours worked within any period of time I give it.
For example if there are two payslips from Jan 2021 - Feb 2021 belonging to Employee 1 and he worked a total of 60 hours within those two months, I want to get 60 hours worked in one row. Same goes for every other employee.
How can I achieve this?
Upvotes: 0
Views: 39
Reputation: 10101
What you need is a JOIN, and GROUP BY, and SUM().
Something like:
SELECT E.EmployeeID, E.Name, Hours = SUM(P.Hours)
FROM Employee E
JOIN PaySlip P ON P.EmployeeID = E.EmployeeID
WHERE P.Date BETWEEN @StartDate AND @EndDate
GROUP BY E.EmployeeID, E.Name
Upvotes: 1