pepperwind / answer_key /kpis /attrition_rate.sql
rasinmuhammed's picture
Pepperwind Trading Co. preview, version 1.0
27ec678 verified
Raw History Blame Contribute Delete
611 Bytes
-- 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