Mit DENSE_RANK wird es deutlich kürzer:
WITH cte AS (
SELECT t.id AS trans_id,
c.id AS customer_id,
c.company, c.first_name, c.last_name, t.due,
DENSE_RANK() OVER (ORDER BY c.company, c.first_name, c.last_name) rn
FROM customers AS c
JOIN transactions AS t ON t.customer_id = c.id
WHERE t.due > 0
AND t.form_type = 'invoice'
)
SELECT * FROM cte
WHERE rn <= 3;
Der Vorteil: Die Bedingungen stehen nur einmal, und die „Top 3 pro Kunde“-Logik steckt vollständig in der Fensterfunktion statt in einem zusätzlichen Subquery mit LIMIT.