EasyWindow Functions

Running count of orders per customer

The query

SQL
SELECT
  cust_id,
  order_id,
  order_date,
  total,
  ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY order_date) AS order_number,
  COUNT(*) OVER (PARTITION BY cust_id) AS total_orders_by_customer
FROM orders
ORDER BY cust_id, order_date;
Tested against PostgreSQL 16

Note

ROW_NUMBER per customer = order sequence. Total count uses PARTITION BY without ORDER BY = whole partition count.

Tables referenced