# The Weight of Everything Before

> Every purchase carries the ones that came before it. Trace the climb.

Canonical URL: <https://datadriven.io/problems/the_weight_of_everything_before>

Domain: SQL · Difficulty: medium · Seniority: mid · Asked in: Databricks

## Problem

An analyst building lifetime-value curves wants every purchase in the `transactions` table tagged with how much that customer had spent so far, counting every purchase of theirs dated on or before it. Return one row per purchase with the customer, the purchase date and that spend to date, customers in id order and each one's purchases from earliest to latest.

## Tables

### transactions

- transaction_id INTEGER
- user_id INTEGER
- product_id INTEGER
- quantity INTEGER
- total_amount REAL
- transaction_date TEXT

Sample rows (10 of 200):

| transaction_id | user_id | product_id | quantity | total_amount | transaction_date |
| --- | --- | --- | --- | --- | --- |
| 1067 | 197 | 1001 | 2 | 23.46 | 2026-02-02 |
| 1134 | 294 | 1050 | 3 | 36.93 | 2026-03-03 |
| 1201 | 391 | 1099 | 4 | 50.4 | 2026-04-04 |
| 1268 | 488 | 1148 | 5 | 63.87 | 2026-05-05 |
| 1335 | 585 | 1197 | 0 | 0 | 2026-06-06 |
| 1402 | 682 | 1246 | 2 | 90.81 | 2026-07-07 |
| 1469 | 779 | 1295 | 3 | 104.28 | 2026-08-08 |
| 1536 | 876 | 1344 | 4 | 117.75 | 2026-09-09 |
| 1603 | 973 | 1393 | 5 | -45 | 2026-01-10 |
| 1670 | 1070 | 1442 | 1 | 144.69 | 2026-02-11 |

## Expected output

First 5 of 200 rows:

| user_id | transaction_date | running_total |
| --- | --- | --- |
| 100 | 2026-01-07 | 1222.29 |
| 100 | 2026-01-18 | 1838.4299999999998 |
| 100 | 2026-03-03 | 2252.52 |
| 100 | 2026-03-20 | 3272.7599999999998 |
| 100 | 2026-04-03 | 3686.85 |

## Worked solution and explanation

### What this really is

Strip off the lifetime-value framing and this is a per-customer running sum that has to keep every purchase row. A `GROUP BY user_id` with `SUM(total_amount)` is the simpler tool and fails on sight: 200 purchases collapse into 15 customer rows, each holding one lifetime number, and the trajectory is gone. The trap that actually separates candidates is subtler. Many reach for a window, write `SUM(total_amount) OVER (PARTITION BY user_id)`, see 200 rows and stop. With no ordering inside the window, every row carries the customer's final total, so a first purchase already shows everything that customer will ever spend.

---

### Break down the requirements

#### Step 1: Keep every purchase row

The output has one row per purchase, not one per customer. That rules out a `GROUP BY` collapse: you need a value computed alongside each existing row while the row count stays at 200.

#### Step 2: Restart the sum for each customer

Partition by `user_id` so one customer's spend never bleeds into another's. Without it you get a single company-wide running sum, which looks plausible for the first customer and wrong for every customer after.

#### Step 3: Order the window by `transaction_date`

The ordering inside `OVER` is what makes the sum cumulative. The default `RANGE` frame then includes every row up to and including the current date, so two purchases on the same day share one value: the spend through that day. The final `ORDER BY user_id, transaction_date` lays each history out chronologically.

---

### The solution

**Per-customer running total**

```sql
SELECT user_id, transaction_date, SUM(total_amount) OVER (PARTITION BY user_id ORDER BY transaction_date) AS running_total FROM transactions ORDER BY user_id, transaction_date;
```

> **One sort, then a single streaming pass**
>
> On an 80M-row `transactions` table of about 25 GB, the dominant cost is the sort by (`user_id`, `transaction_date`). After that the window streams each customer once, with memory bounded by a single partition. A self-join alternative pairs every purchase with every earlier one, which is quadratic per customer and explodes for heavy buyers.

> **Same-day purchases are peers, not a sequence**
>
> Strong candidates raise ties on `transaction_date` before they are asked. They explain that the default `RANGE` frame gives same-day purchases one shared value, and that `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` would split them in an arbitrary order unless a tiebreaker like `transaction_id` is added.

> **No ordering means every row shows the final total**
>
> `SUM(total_amount) OVER (PARTITION BY user_id)` keeps 200 rows, so it passes a quick row-count check. Every value in it is the customer's lifetime total, though, so the curve is flat, and that is easy to miss if you only eyeball the first few rows.

---

## Common follow-up questions

- How would you show the spend across only each customer's last three purchases, a moving window rather than a cumulative one? _(Tests switching to an explicit `ROWS BETWEEN 2 PRECEDING AND CURRENT ROW` frame.)_
- If two purchases share the same `transaction_date` for a customer, what `running_total` does each row show, and how would you make the accumulation strictly row by row? _(Tests `RANGE` versus `ROWS` framing and a deterministic tiebreaker such as `transaction_id`.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_weight_of_everything_before)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.