MySQL: Easy, ONLY 3 Lines Using SUM-IF
SELECT stock_name, SUM(IF(operation = "Buy", price*(-1), price)) capital_gain_loss\nFROM stocks\nGROUP BY stock_name
SELECT stock_name, SUM(IF(operation = "Buy", price*(-1), price)) capital_gain_loss\nFROM stocks\nGROUP BY stock_name
['MySQL']