>_ QueryLocal All recipes
SQL recipe

How do I number each customer’s orders in date order with SQL?

SELECT
  customer,
  order_date,
  ROW_NUMBER() OVER (PARTITION BY customer ORDER BY order_date) AS order_sequence
FROM data
ORDER BY customer, order_sequence;
Run this query in QueryLocal →

`ROW_NUMBER()` assigns a strictly increasing integer (1, 2, 3, ...) within each partition, with no ties — here it numbers each customer’s orders in the order they were placed, which is useful for questions like "which was each customer’s first order".

A table named data with columns: order_id (integer), customer (text), country (text), category (text), quantity (integer), unit_price (real), order_date (text, YYYY-MM-DD).