#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

Sagaidāmais izvades paraugs

Tā izskatās pareiza atbilde — tās rindu skaits ir pašai savs, tam nav jāatbilst shēmas tabulām.

user_idpaid_count
12
21

Tava vaicājuma rezultāts parādīsies šeit