3/34
Part 1 of 6 · WindowsLearn and do
From GROUP BY to OVER
The loader's query was a GROUP BY, and it collapsed each user's orders into one row:
SELECT user_id, COUNT(*) AS orders, SUM(amount) AS spend
FROM orders
GROUP BY user_id
ORDER BY user_id;
| user_id | orders | spend |
|---|---|---|
| 1 | 3 | 120 |
| 2 | 2 | 55 |
| 3 | 1 | 25 |
| 4 | 1 | 50 |
You get the totals and lose the orders. Now put OVER after the same SUM.
GROUP BY user_id returned 4 rows. How many rows does SUM(amount) OVER (PARTITION BY user_id) return?
Pick an answer above to see the rest