Position Over Time
Trades happen in order of id. A buy adds its qty to the position in its symbol and a sell takes it away. Return every trade's id and symbol, and position: the position in that symbol right after the trade. In order of id.
Example 1
trades
| id | symbol | side | qty |
|---|---|---|---|
| 1 | AAPL | buy | 10 |
| 2 | MSFT | buy | 5 |
| 3 | AAPL | sell | 4 |
| 4 | AAPL | sell | 9 |
| 5 | MSFT | buy | 1 |
returns
| id | symbol | position |
|---|---|---|
| 1 | AAPL | 10 |
| 2 | MSFT | 5 |
| 3 | AAPL | 6 |
| 4 | AAPL | -3 |
| 5 | MSFT | 6 |
Constraints
- Up to 16 trades
sideisbuyorsell- Ids are unique
Write it in your browser, against the clock. The model query, with a note on each piece of it, comes after.
More SQL problems
- SessionsHard
- Three Days RunningHard
- Top Two Salaries per DepartmentHard
- Worst DrawdownHard
- Between Two PricesBeginner
- Both ConditionsBeginner
- Cheapest FirstBeginner
- Each Symbol OnceBeginner