File size: 611 Bytes
27ec678 | 1 2 3 4 | -- Monthly attrition rate
-- Employees employed at the month's start whose leaving day falls in it, over those employed at its start.
WITH m AS (SELECT strftime(range, '%Y-%m') AS month, range AS t0, range + INTERVAL 1 MONTH AS t FROM range(TIMESTAMP '2023-10-01', TIMESTAMP '2026-10-01', INTERVAL 1 MONTH)) SELECT m.month, count(*) FILTER (WHERE e.leave_date < m.t) AS left_in_month, count(*) AS headcount_at_start, count(*) FILTER (WHERE e.leave_date < m.t)::DOUBLE / count(*) AS value FROM m JOIN core.employee e ON e.hire_date < m.t0 AND (e.leave_date IS NULL OR e.leave_date >= m.t0) GROUP BY 1 ORDER BY 1
|