Employee Productivity
Write a procedure employee_productivity(p_from, p_to) that ranks employees by a fitness score over a period, F = F1 + F2 + F3:
- F1 = customers the employee served / customers who ordered in the period;
- F2 = days the employee handled at least one order / days with at least one order;
- F3 = the employee's orders / all orders in the period.
Print each employee with F and its three parts, best first.
Uses the Northwind sample data: load ../northwind/northwind.sql first, then run solution.sql.
Example
employee_productivity(DATE '1996-01-29', DATE '1997-05-29'):
Peacock Margaret F=0.985 (0.543, 0.242, 0.200)
Callahan Laura F=0.771 (0.444, 0.182, 0.144)
Leverling Janet F=0.756 (0.407, 0.195, 0.154)
Davolio Nancy F=0.710 (0.407, 0.165, 0.138)
Fuller Andrew F=0.559 (0.333, 0.127, 0.098)
King Robert F=0.540 (0.333, 0.114, 0.092)
Suyama Michael F=0.475 (0.272, 0.114, 0.089)
Buchanan Steven F=0.293 (0.173, 0.068, 0.052)
Dodsworth Anne F=0.199 (0.123, 0.042, 0.033)
Approach
Every part is a ratio of a per-employee count to the same count over the whole period, so one query computes both: a CTE p with the orders in the period, a one-row CTE tot with the three totals, and a GROUP BY employee over p cross-joined with tot. Ordering by f1 + f2 + f3 puts the ranking in SQL.
The denominators are distinct counts: count(DISTINCT CustomerID) and count(DISTINCT OrderDate), not the number of orders. Computing them once, instead of in a query per employee, is the main difference from a cursor-per-employee solution.
Lesson: grouping, a cursor loop vs one statement.
Solutions
-- Needs the Northwind tables: run ../northwind/northwind.sql first.
SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
CREATE OR REPLACE PROCEDURE employee_productivity(p_from DATE, p_to DATE) IS
BEGIN
FOR e IN (
WITH p AS (
SELECT * FROM orders WHERE OrderDate BETWEEN p_from AND p_to
), tot AS (
SELECT count(DISTINCT CustomerID) AS customers,
count(DISTINCT OrderDate) AS work_days,
count(*) AS orders
FROM p
)
SELECT e.LastName, e.FirstName,
count(DISTINCT p.CustomerID) / tot.customers AS f1,
count(DISTINCT p.OrderDate) / tot.work_days AS f2,
count(*) / tot.orders AS f3
FROM p
JOIN employees e ON e.EmployeeID = p.EmployeeID
CROSS JOIN tot
GROUP BY e.EmployeeID, e.LastName, e.FirstName, tot.customers, tot.work_days, tot.orders
ORDER BY f1 + f2 + f3 DESC
) LOOP
DBMS_OUTPUT.PUT_LINE(e.LastName || ' ' || e.FirstName
|| ' F=' || TO_CHAR(e.f1 + e.f2 + e.f3, 'FM0.000')
|| ' (' || TO_CHAR(e.f1, 'FM0.000') || ', ' || TO_CHAR(e.f2, 'FM0.000')
|| ', ' || TO_CHAR(e.f3, 'FM0.000') || ')');
END LOOP;
END;
/
EXEC employee_productivity(DATE '1996-01-29', DATE '1997-05-29')