← Back to SQL, page 22

sql-0421 · Medium mid

For each month, count customers whose first order was in that month, and show the cumulative total.

First find each customer's first order month with MIN(order_date) grouped by customer. Then count customers per first month and add a running SUM over the months for the cumulative customer base.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
WITH firsts AS (
  SELECT customer_id, strftime('%Y-%m', MIN(order_date)) AS first_month FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id
)
SELECT first_month, COUNT(*) AS new_customers, SUM(COUNT(*)) OVER (ORDER BY first_month) AS cumulative
FROM firsts GROUP BY first_month ORDER BY first_month;
first_monthnew_customerscumulative
2026-0111
2026-0212
2026-0313

Ana first ordered in January, Bilal in February and Chloe in March, so each month gains one new customer and the running total climbs 1, 2, 3. The window SUM(COUNT(*)) OVER (...) runs after grouping, summing the per-month counts. Months with no new customers would be missing; join to a month calendar when the chart needs every month. Excluding orders without a customer keeps the anonymous order 106 from counting as a customer.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

WITH firsts AS (
  SELECT customer_id, strftime('%Y-%m', MIN(order_date)) AS first_month FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id
)
SELECT first_month, COUNT(*) AS new_customers, SUM(COUNT(*)) OVER (ORDER BY first_month) AS cumulative
FROM firsts GROUP BY first_month ORDER BY first_month;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Find first purchase per customer first
  • Count by first month
  • Window over grouped results for cumulative totals
  • Use a calendar for empty months

Common mistakes

  • Counting distinct customers per month, which repeats returning customers
  • Including orders with no customer

Follow-ups

  • How would you count returning customers per month?
  • How would you add revenue from new customers?

Tips

  • Separate first purchase from later orders