Uma abordagem seria usar visualizações em linha. Para fazer isso
- Crie uma consulta que some os pontos ganhos
- Crie uma consulta que some os pontos gastos
- Junte as consultas (uma junção à esquerda caso ainda não tenha gasto nada)
- Faça sua subtração
SELECT SumOfPointsEarned.Points - COALESCE(SumOfPointsOfPurchasesMade.Points, 0) AS CurrentPoints
FROM
(
SELECT SUM(Points) AS Points, Recipient_ID
FROM transactions
WHERE Recipient_ID= 137642
GROUP BY Recipient_ID
) AS SumOfPointsEarned
LEFT JOIN
(
SELECT purchases.Student_ID, SUM(rewards.Cost_to_User) AS Points
FROM purchases
INNER JOIN rewards
ON purchases.Reward_ID = rewards.Reward_ID
WHERE Student_ID = 137642
GROUP BY purchases.Student_ID
) AS SumOfPointsOfPurchasesMade
ON SumOfPointsEarned.Recipient_ID = SumOfPointsOfPurchasesMade.Student_ID