I want to SUM t.amount, w.bStake and w.lStake (conditionally)
from transactions t and wagers w.
The amount should always be summed up and between back and profit should be a condition.
If w.Profit is positiv -> t.Amount + w.Back
If w.Profit is negativ -> t.Amount + w.Profit
My code that I use until this point looks like this:
SELECT
b.Bookie,
(SELECT SUM(t.Amount) FROM Transactions t WHERE t.Bookie = b.id)
FROM Bookie b
My tables are:
Bookie:
------------
Id : int (Primary Key)
Bookie : varchar
Transactions:
-------------------
Id : int (Primary Key)
Date : date
Bookie : int (Foreign Key)
Amount : decimal
Wagers:
Id : int (Primary Key)
Profit : decimal
Back : decimal
Bookie : int (Foreign Key)
How can I solve this Issue? Or is it only possible if I restructure my database? Thanks a lot.
sum(t.Amount + case when w.Profit >=0 then w.Back else w.Profit end)