Xorthax noticed something interesting—looking at totals across all customers wasn’t always helpful. He needed to analyze each customer separately.
That’s where PARTITION BY comes in.
You already know that OVER() lets a function look across rows.
PARTITION BY changes which rows it looks at.
FUNCTION(...) OVER (
PARTITION BY column
)
Instead of using all rows, the function now:
| OrderID | CustomerID | Amount |
|---|---|---|
| 1 | A | 100 |
| 2 | A | 150 |
| 3 | B | 200 |
| 4 | B | 50 |
SELECT OrderID, CustomerID, Amount,
SUM(Amount) OVER (PARTITION BY CustomerID) AS CustomerTotal
FROM Orders;
Result:
| OrderID | CustomerID | Amount | CustomerTotal |
|---|---|---|---|
| 1 | A | 100 | 250 |
| 2 | A | 150 | 250 |
| 3 | B | 200 | 250 |
| 4 | B | 50 | 250 |
Each customer now has their own total, calculated separately.
PARTITION BY CustomerID splits the data into groups:
Then SUM() runs inside each group and attaches the result to every row in that group.
The calculation resets for each partition.
For each order, show the total revenue for that customer using a window function.
| Column | Type |
|---|---|
| orderid | bigint |
| customerid | bigint |
| orderdate | timestamp without time zone |
| totalamount | double precision |
| status | text |
| Column |
|---|
| customerid |
| orderid |
| totalamount |
| customer_total |
Right now, each group is treated as a whole.
Next, you’ll learn how to control the order of rows within each group.