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
Várt kimeneti minta
Így néz ki egy helyes válasz — a sorszáma a sajátja, nem kell egyeznie a sématáblákkal.
| user_id | paid_count |
|---|---|
| 1 | 2 |
| 2 | 1 |
Jelentkezz be a beküldési előzmények megtekintéséhez
BejelentkezésJelentkezz be az MI-mentor használatához
Bejelentkezés