Easiest solution possible. Easy to replicate.
Approach\nIt\'s very easy solution. Started with separating thing which are going to get aggregated. All the items in \'buy\' section will give negative value a
# Approach\nIt\'s very easy solution. Started with separating thing which are going to get aggregated. All the items in \'buy\' section will give negative value and \'sell\' section gives positive. So, did separate calculations first.\nthen in 2nd round I Grouped the things according to the stock_name.\n\n\n# Code\n```\n# Write your MySQL query statement below\nwith cte as (\nselect stock_name, operation, case when operation =\'Buy\' then -1*sum(price) \nwhen operation =\'Sell\' then sum(price) End as total_price\nfrom Stocks \ngroup by stock_name, operation)\nselect stock_name, sum(total_price) as capital_gain_loss\nfrom cte\ngroup by stock_name;\n```
['MySQL']