I have a table called Registers with this structure:
What I do is add a new column to the right where a balance is going to be calculated (either add or subtract) based on the value of each record in the Concept column; something like this:
As you can see, I need the calculation to start from the amount of the first record of each ID, that is, as if it were making a cut and based on the subsequent records the calculation is made. For example when the concept is 1 you must add and for 2 and 3 you must subtract.
I have this query but it marks me an error in order and if I remove it in the additional column, balance puts the same amount in all the records, that is, neither adds nor subtracts: (
SELECT
ID, Concepto, Fecha, Importe,
SUM(Importe * case when Concepto = 1 then 1 else -1 end)
OVER(PARTITION BY ID ORDER BY Fecha ) AS Saldo
FROM Registros
ORDER BY ID, Fecha;