#998

Paid Orders per Buyer

Analytics needs, for each buyer who has orders with status = 'paid', the number of those orders. Replace the raw repeated-row approach with one row per user_id. Return user_id and paid_count, sorted by user_id.

Original query
SELECT DISTINCT o.user_id, (SELECT COUNT(*) FROM orders o2 WHERE o2.user_id = o.user_id AND o2.status='paid') AS paid_count FROM orders o WHERE o.status='paid' AND o.user_id IS NOT NULL ORDER BY o.user_id

Δείγμα αναμενόμενης εξόδου

Έτσι μοιάζει μια σωστή απάντηση — το πλήθος γραμμών της είναι δικό της, δεν χρειάζεται να ταιριάζει με τους πίνακες του σχήματος.

user_idpaid_count
12
21

Το αποτέλεσμα του ερωτήματός σας θα εμφανιστεί εδώ