Robinhood needs to display the real-time total value of every user portfolio. The total value is calculated by taking the net shares owned for each ticker and multiplying it by the most recent market price for that ticker.
Write an SQL query to calculate the current total market value of each user portfolio.
| user_id(INTEGER) | ticker(VARCHAR) | transaction_type(VARCHAR) | quantity(INTEGER) |
|---|---|---|---|
| 1 | AAPL | BUY | 10 |
| 1 | AAPL | SELL | 2 |
| 1 | TSLA | BUY | 5 |
| 2 | AAPL | BUY | 15 |
| 2 | BTC | BUY | 1 |
| 3 | TSLA | BUY | 10 |
| 3 | TSLA | SELL | 10 |
| ticker(VARCHAR) | current_price(DECIMAL) | price_timestamp(TIMESTAMP) |
|---|---|---|
| AAPL | 180 | 2026-02-11 09:00:00 |
| AAPL | 185.5 | 2026-02-11 10:00:00 |
| TSLA | 200 | 2026-02-11 10:00:00 |
| BTC | 45000 | 2026-02-11 10:00:00 |
| user_id(INTEGER) | total_portfolio_value(DECIMAL) |
|---|---|
| 2 | 47782.5 |
| 1 | 2484 |