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_idordersspend
13120
2255
3125
4150

You get the totals and lose the orders. Now put OVER after the same SUM.

Predict

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