Open Positions
A buy adds its qty to the position in a symbol and a sell takes it away. Return each symbol whose position after all trades is not zero, with that position (negative when more was sold than bought), in alphabetical order of symbol.
Example 1
trades
| id | symbol | side | qty | price |
|---|---|---|---|---|
| 1 | MSFT | buy | 10 | 410.0 |
| 2 | AAPL | sell | 4 | 191.0 |
| 3 | MSFT | sell | 10 | 412.0 |
| 4 | AAPL | buy | 1 | 189.0 |
| 5 | KO | buy | 7 | 61.0 |
returns
| symbol | position |
|---|---|
| AAPL | -3 |
| KO | 7 |
Constraints
- Up to 16 trades
sideisbuyorsell
Write it in your browser, against the clock. The model query, with a note on each piece of it, comes after.
More SQL problems
- Rank in the DepartmentMedium
- Revenue per CityMedium
- Running BalanceMedium
- Second From the TopMedium
- Share of the TotalMedium
- Volume Weighted PriceMedium
- Chain of CommandHard
- Daily BarsHard