Easiest and precise - group by + if + sum
You can also use any other method but this is the easiest to understand and precise to the question asked.\n\n\nselect stock_name, sum(if(operation="Buy", - pri
You can also use any other method but this is the easiest to understand and precise to the question asked.\n\n```\nselect stock_name, sum(if(operation="Buy", - price, price)) as capital_gain_loss\nfrom stocks\ngroup by stock_name\norder by stock_name\n```
['MySQL']