Customer Tiers

Write a procedure customer_tiers(p_min, p_max) that lists every customer with their number of orders and a tier: high if they placed more than p_max orders, medium from p_min to p_max, low below p_min. Most orders first, ties by name.

Uses the Northwind sample data: load ../northwind/northwind.sql first, then run solution.sql.

Example

customer_tiers(5, 15), first and last rows:

Save-a-lot Markets: 31 orders, high
Ernst Handel: 30 orders, high
QUICK-Stop: 28 orders, high
...
Bon app': 17 orders, high
Frankenversand: 15 orders, medium
Lehmanns Marktstand: 15 orders, medium
...
Lazy K Kountry Store: 2 orders, low
Centro comercial Moctezuma: 1 orders, low
FISSA Fabrica Inter. Salchichas S.A.: 0 orders, low
Paris spécialités: 0 orders, low

Approach

One grouped query does the counting and the ordering; the loop only formats. The tier is a CASE on the count.

The boundaries need care: 15 orders with p_max = 15 is medium, so the test for high is > p_max and for medium >= p_min.

LEFT JOIN keeps the two customers who never ordered, with a count of 0. With an inner join they disappear from the list, which is wrong for a report that is supposed to list every customer. count(o.OrderID) (not count(*)) is what makes their count 0 instead of 1.

Lesson: grouping, PL/SQL cursors.

Solutions

-- Needs the Northwind tables: run ../northwind/northwind.sql first.
SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF

CREATE OR REPLACE PROCEDURE customer_tiers(p_min NUMBER, p_max NUMBER) IS
BEGIN
    FOR c IN (SELECT c.CompanyName, count(o.OrderID) AS n
              FROM customers c
              LEFT JOIN orders o ON o.CustomerID = c.CustomerID
              GROUP BY c.CustomerID, c.CompanyName
              ORDER BY n DESC, c.CompanyName) LOOP
        DBMS_OUTPUT.PUT_LINE(c.CompanyName || ': ' || c.n || ' orders, ' ||
            CASE WHEN c.n > p_max  THEN 'high'
                 WHEN c.n >= p_min THEN 'medium'
                 ELSE 'low' END);
    END LOOP;
END;
/

EXEC customer_tiers(5, 15)