EasyWindow Functions

LEAD: see next order total per customer

The query

SQL
SELECT
  cust_id,
  order_id,
  order_date,
  total,
  LEAD(total) OVER (PARTITION BY cust_id ORDER BY order_date) AS next_order_total,
  LEAD(order_date) OVER (PARTITION BY cust_id ORDER BY order_date) AS next_order_date
FROM orders
ORDER BY cust_id, order_date;
Tested against PostgreSQL 16

Note

LEAD looks ahead. PARTITION BY cust_id resets for each customer — orders compared within same customer.

Tables referenced