easy 3 lines solution
select stock_name, sum(case when operation = \'Buy\' then -price else price end) as capital_gain_loss \nfrom Stocks\ngroup by stock_name;
select stock_name, sum(case when operation = \'Buy\' then -price else price end) as capital_gain_loss \nfrom Stocks\ngroup by stock_name;
[]