WITH CustomerPayments AS (
SELECT c.customerNumber,
c.customerName,
SUM(amount) AS total
FROM customers c
LEFT JOIN payments p ON c.customerNumber = p.customerNumber
GROUP BY c.customerNumber, c.customerName
)
SELECT customerName,
total,
CASE
WHEN total >= 100000 THEN "high-valued"
WHEN total < 100000 AND total > 0 THEN "medium-valued"
ELSE "low-valued"
END AS priority
FROM CustomerPayments
ORDER BY total DESC;