Given a dataset of financial transactions, write an optimized SQL query using window functions to calculate the running balance of a user's portfolio over time at Scalable Capital.
Use completed transactions with non-null amounts and valid users. Deposits and sells increase the balance, while withdrawals and buys decrease it.
user_id, full_name, transaction_id, transaction_timestamp, transaction_type, signed_amount, and running_balance.user_id, transaction_timestamp, then transaction_id.| Column | Type | Description |
|---|---|---|
| user_idPK | INT | Unique Scalable Capital customer identifier |
| full_name | VARCHAR(100) | Customer's display name |
| risk_profile | VARCHAR(30) | Customer investment risk profile |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| user_id | INT | Customer associated with the transaction |
| transaction_timestamp | TIMESTAMP | Time when the transaction occurred |
| transaction_type | VARCHAR(20) | Deposit, withdrawal, buy, or sell |
| amount | DECIMAL(12,2) | Unsigned transaction amount |
| status | VARCHAR(20) | Transaction processing status |