1393.sql (212B)
1 SELECT 2 stock_name, 3 SUM( 4 CASE 5 WHEN operation = 'buy' THEN -price 6 WHEN operation = 'sell' THEN price 7 END 8 ) AS capital_gain_loss 9 FROM Stocks 10 GROUP BY stock_name