D8LooPFocus modeCODE WORKSPACE
Return order_id and snapshot_count for every order_id that appears more than once in order_snapshots.
Group by the key that should be unique, then keep the groups whose count is above one.
Sample input
| order_id | status | updated_at |
|---|---|---|
| 1001 | paid | 2026-01-12 09:00:00 |
| 1001 | shipped | 2026-01-13 10:00:00 |
| 1002 | paid | 2026-01-12 10:30:00 |
| 1004 | paid | 2026-01-15 14:30:00 |
| 1004 | shipped | 2026-01-16 12:20:00 |
| 1008 | shipped | 2026-01-26 10:30:00 |
Expected output
| order_id | snapshot_count |
|---|---|
| 1001 | 2 |
| 1004 | 2 |
Constraints
GROUP BY order_id and keep only groups with COUNT(*) > 1. Order by snapshot_count DESC, then order_id ASC.
Expected skills
Duplicate-key detection with GROUP BY and HAVING.
Run the SQL query to inspect preview rows.