D8LooPFocus modeCODE WORKSPACE
Return status and buyers (the count of distinct buyer_id values) for each status. Order by buyers descending, then status ascending.
GROUP BY status with COUNT(DISTINCT buyer_id) AS buyers, then ORDER BY buyers DESC, status ASC.
Sample input
| order_id | buyer_id | status |
|---|---|---|
| 1001 | 1 | paid |
| 1002 | 3 | paid |
| 1005 | 2 | paid |
| 1007 | 3 | paid |
| 1010 | 6 | paid |
| 1011 | 2 | paid |
Expected output
| status | buyers |
|---|---|
| paid | 4 |
| shipped | 3 |
| pending | 2 |
| refunded | 1 |
Constraints
GROUP BY status. Use COUNT(DISTINCT buyer_id) AS buyers so repeat buyers are counted once. Order by buyers DESC, status ASC.
Expected skills
Counting unique entities with COUNT(DISTINCT ...) inside groups.
Run the SQL query to inspect preview rows.