Write a SQL query using window functions to find the latest record per user and calculate a running total.
Use the user_records table. Calculate the running total of amount separately for each user, treating NULL amounts as zero. If timestamps tie, the record with the greater record_id is later.
user_id, record_id, recorded_at, amount, and running_totaluser_id ascending| Column | Type | Description |
|---|---|---|
| record_idPK | INT | Unique record identifier |
| user_id | INT | Identifier of the user associated with the record |
| recorded_at | TIMESTAMPTZ | Timestamp when the record was created |
| amount | NUMERIC(12,2) | Amount included in the user's cumulative total |