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
Esempio di output atteso
Ecco come appare una risposta corretta — il suo numero di righe è a sé stante, non deve corrispondere alle tabelle dello schema.
| user_id | paid_count |
|---|---|
| 1 | 2 |
| 2 | 1 |
Accedi per vedere la cronologia degli invii
AccediAccedi per usare l'AI Mentor
Accedi