SQL query to get net profit stock wise
You can use a case
expression to count "sell"s as profits and "buy"s as loses, and then sum them:
SELECT stock, SUM(CASE type WHEN 'sell' THEN sval
WHEN 'buy' THEN sval * -1
END) as "profit/loss"
FROM stocks
GROUP BY stock