MySQL window function
with t1 as \n(\n    select\n        *,\n        row_number() over(partition by stock_name order by operation_day) as row_num,\n        lag(price,1) over(partiti
with t1 as \n(\n    select\n        *,\n        row_number() over(partition by stock_name order by operation_day) as row_num,\n        lag(price,1) over(partition by stock_name order by operation_day) as lag_price    \n    from Stocks\n)\nselect\n   stock_name,\n   sum(price-lag_price) as capital_gain_loss\nfrom t1\nwhere mod(row_num,2)=0\ngroup by 1;
[]