Calculating Monthly Overtime in SQL Server for Garment Factory Payroll
Overtime is where payroll errors hide. In a garment factory with hundreds or thousands of workers, one wrong rounding rule multiplied by every worker and every day becomes a real amount of money — and a compliance problem. This article shows a clean way to calculate monthly overtime in SQL Server from raw attendance, with every rule in one place.
The numbers below follow a common practice in Bangladeshi garment factories: overtime is paid at twice the hourly basic wage, and the hourly basic is the monthly basic ÷ 208 (26 working days × 8 hours). Always confirm the formula with your company policy and the labour law that applies to you.
The tables
CREATE TABLE dbo.Employees (
EmployeeId INT PRIMARY KEY,
FullName NVARCHAR(100) NOT NULL,
BasicSalary DECIMAL(12, 2) NOT NULL -- monthly basic
);
CREATE TABLE dbo.Attendance (
EmployeeId INT NOT NULL REFERENCES dbo.Employees (EmployeeId),
WorkDate DATE NOT NULL,
InTime DATETIME2(0) NOT NULL,
OutTime DATETIME2(0) NULL, -- NULL = missing out-punch
CONSTRAINT PK_Attendance PRIMARY KEY (EmployeeId, WorkDate)
);
Storing full date-times instead of just times means night shifts that end after midnight need no special handling — DATEDIFF simply counts the minutes between the two moments.
The rules, as variables
Numbers hard-coded across a long query are what make payroll hard to audit. Put the rules at the top, where HR and auditors can read them:
DECLARE @From DATE = '2026-09-01',
@To DATE = '2026-09-30',
@StandardMinutes INT = 8 * 60, -- regular working time per day
@BreakMinutes INT = 60, -- unpaid lunch break
@BlockMinutes INT = 30, -- overtime is counted in 30-minute blocks
@HoursPerMonth DECIMAL(6, 2) = 208,
@OtMultiplier DECIMAL(4, 2) = 2;
Step 1: worked minutes per day
WITH Daily AS (
SELECT a.EmployeeId,
a.WorkDate,
WorkedMinutes = DATEDIFF(MINUTE, a.InTime, a.OutTime) - @BreakMinutes
FROM dbo.Attendance AS a
WHERE a.WorkDate BETWEEN @From AND @To
AND a.OutTime IS NOT NULL
),
Days with a missing out-punch are left out here. Handle them separately (see the checks below) instead of letting the query guess.
Step 2: overtime per day, rounded down to full blocks
Overtime AS (
SELECT EmployeeId,
WorkDate,
OtMinutes = CASE
WHEN WorkedMinutes > @StandardMinutes
THEN (WorkedMinutes - @StandardMinutes) / @BlockMinutes * @BlockMinutes
ELSE 0
END
FROM Daily
)
Because both values are integers, / @BlockMinutes * @BlockMinutes rounds down to a full block: 29 extra minutes count as 0, 45 count as 30 and 61 count as 60.
Step 3: monthly hours and amount
SELECT e.EmployeeId,
e.FullName,
OtHours = CAST(SUM(o.OtMinutes) / 60.0 AS DECIMAL(6, 2)),
OtRate = CAST(e.BasicSalary / @HoursPerMonth * @OtMultiplier AS DECIMAL(12, 2)),
OtAmount = CAST(SUM(o.OtMinutes) / 60.0 * e.BasicSalary / @HoursPerMonth * @OtMultiplier
AS DECIMAL(12, 2))
FROM dbo.Employees AS e
JOIN Overtime AS o ON o.EmployeeId = e.EmployeeId
GROUP BY e.EmployeeId, e.FullName, e.BasicSalary
ORDER BY e.EmployeeId;
Run the variables and the three parts together as one batch. For a worker with a monthly basic of 8,000, the hourly overtime rate is 8,000 ÷ 208 × 2 ≈ 76.92, so 40.5 overtime hours pay about 3,115.
Checks worth running before payroll is final
A calculation is only as good as the attendance data behind it. Two checks worth running every month:
-- 1. Missing out-punches: fix these before paying overtime
SELECT EmployeeId, WorkDate, InTime
FROM dbo.Attendance
WHERE WorkDate BETWEEN @From AND @To
AND OutTime IS NULL;
-- 2. Days above your daily overtime limit (here: 2 hours)
SELECT EmployeeId, WorkDate, WorkedMinutes - @StandardMinutes AS ExtraMinutes
FROM (
SELECT EmployeeId,
WorkDate,
WorkedMinutes = DATEDIFF(MINUTE, InTime, OutTime) - @BreakMinutes
FROM dbo.Attendance
WHERE WorkDate BETWEEN @From AND @To
AND OutTime IS NOT NULL
) AS d
WHERE WorkedMinutes - @StandardMinutes > 120;
Buyer compliance audits look closely at excessive overtime, so it is better to see these rows before an auditor does.
Performance
For a factory-sized attendance table, an index that matches the date filter keeps the monthly run fast:
CREATE INDEX IX_Attendance_WorkDate
ON dbo.Attendance (WorkDate)
INCLUDE (EmployeeId, InTime, OutTime);
Where to go next
Wrap the query in a stored procedure that takes @From and @To, add holiday rules (on weekly holidays, often every worked hour counts as overtime), and feed the result into the salary sheet or an RDLC report. Because the rules live in parameters, the next policy change is a one-line edit instead of a hunt through the code.


