easy solution using case when
Code\n\nselect \n    stock_name, \n    sum(case when operation  = \'Sell\' then price else price*-1 end) as capital_gain_loss\nfrom Stocks\ngroup by stock_name;
# Code\n```\nselect \n    stock_name, \n    sum(case when operation  = \'Sell\' then price else price*-1 end) as capital_gain_loss\nfrom Stocks\ngroup by stock_name;\n```
['MS SQL Server']