Why it happens: ROW_NUMBER does not work in WHERE
SELECT customer_id,
date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY date DESC, id DESC
) AS position
FROM orders
WHERE ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY date DESC, id DESC
) = 1;The intent is reasonable, but the WHERE stage does not have the window's result yet.
How to fix it in SQL
Compute the window in an inner layer
SELECT customer_id, date
FROM (
SELECT customer_id,
date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY date DESC, id DESC
) AS position
FROM orders
) AS sorted
WHERE position = 1;The inner query produces a virtual table that already contains position. In the outer query, that name is an ordinary column and WHERE can filter it.
The same approach with a CTE
WITH sorted AS (
SELECT customer_id,
date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY date DESC, id DESC
) AS position
FROM orders
)
SELECT customer_id, date
FROM sorted
WHERE position = 1;The CTE does not change the logic: it just makes the split between calculating and filtering more visible.
How to check it
Keep the position column at first, and check the order and any ties within each group.
How to avoid it next time
HAVING cannot stand in for the outer query when filtering a window.
Related cases worth checking
If several rows can share the same date, add a stable tiebreaker such as id DESC. Otherwise, which of the tied rows gets ROW_NUMBER() = 1 may not be a deterministic choice.
Filtering before or after numbering answers different questions
Splitting the query in two is not just a trick to avoid an error message: it decides which rows take part in the numbering. If you want each customer's latest paid order, the status filter goes inside the query that computes the window. If you want customers whose latest order, whatever its status, is paid, number every order and filter on status in the outer query together with the position.
Take a customer with an order paid on the 10th and another cancelled on the 12th. The first report shows the order from the 10th. In the second report, that customer does not appear at all, because their latest order is cancelled. Both queries run without errors and give different results; checking only that the error message has gone does not validate the business question.
Keep the order key in the diagnostic output so you can see which order won, not just its date. The ordering inside OVER decides the position, but on its own it does not sort the final report. Add an outer ORDER BY if you need a stable presentation. If every order tied on the most recent date should survive, consider a ranking function instead of picking a single row.
Self-contained case to check in SQLite
The data contains an older payment and a later cancellation from the same customer. Here we ask for their latest paid order.
WITH orders(id, customer_id, date, status) AS (
VALUES (10, 1, '2026-10-10', 'paid'),
(12, 1, '2026-10-12', 'cancelled')
), sorted AS (
SELECT id, customer_id, status,
ROW_NUMBER() OVER (
PARTITION BY customer_id ORDER BY date DESC, id DESC
) AS position
FROM orders WHERE status = 'paid'
)
SELECT id, customer_id, status FROM sorted WHERE position = 1;| id | customer_id | status |
|---|---|---|
| 10 | 1 | paid |
The cancellation does not take part in the window because the filter is inside. Numbering both orders first and then filtering on status and position would return zero rows.
Check your interpretation: Which order gets position 1 if you remove the filter from the inner query?
Show answer
Order 12, the cancellation, because it is the customer's most recent order. If you then filter status = 'paid' outside, the customer disappears from the result.
Technical questions: SQLite: window functions (opens in a new tab).