MS SQL Solution
\n--If you buy the good then it should be treated as a deduction i.e. convert to negative\n--If you sell the good then it should be treated as a gain i.e. leave
```\n--If you buy the good then it should be treated as a deduction i.e. convert to negative\n--If you sell the good then it should be treated as a gain i.e. leave it as a postive value\nSELECT stock_name, SUM(CASE WHEN operation = \'Buy\' THEN price *-1 ELSE price END) as capital_gain_loss\nFROM Stocks\nGROUP BY stock_name\n```
['MS SQL Server']