Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i have trouble in counting balance soo i am doing like this i am trying to select recoreds and then count balance.
Table1
ID
Name
Value(money)
Type(in, Out)
Table2
ID
StartBalance(this is constant and it's value 100)

soo when i am selecting table i need to make temporary column where will bee balance of each record of Table 1 and it must look on in and out Type for example:
Table 1
1 2
Peter Jhon
34$ 20$
IN Out

Starting balance i know that is 100$
so this select must look like:
1
Peter
34$
IN
(temp Col Balance)=134$
Next record
2
Jhon
20$
Out
(temp Col Balance)=114$
plz help me to do this select or some procedure for this

What I have tried:

i have googled it but can't find solution
Posted
Comments
F-ES Sitecore 25-May-17 5:57am    
Google for "sql running total" for examples on how to do these sorts of things
GTR0123 25-May-17 6:18am    
SELECT ID ,
Value ,
( SELECT SUM(y.Value)
FROM dbo.Reestr y
WHERE y.ID <= x.ID

) AS RunningTotal
FROM dbo.Reestr x
ORDER BY 1 ,2 ,3;

i made this but problem is that here is only sum but if Type is(out) i need to minus it and if it type is (In) i need to sum thats the problem :/
GTR0123 25-May-17 7:26am    
SELECT ID ,
Value ,
(select sum(case when Op_Type_Id = 1 then Value when Op_Type_Id=2 then
(0-value) end) FROM dbo.Reestr y where x.Id>=y.id) AS Balance
FROM dbo.Reestr x
ORDER BY 1 ,2 ,3;
found solution
Maciej Los 26-May-17 2:21am    
You have to provide sample data and expected result if you want our help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900