Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello guys i looked some sql server running total examples but i can't manage thing like this :
i have table where i have id,name,type of operation,date,value
so i want to calculate balanace for each recored , balance must be calculted like this starting balance must be 0 and then if operation type is 'IN' there will be plus if operation out then minus and it goes like every next recored must see previus record balance and then +value or -value (depends on operation Type) and this operetion must by ordered by date (not Id) for example table is like this:
C#
ID Name  Op_Type Date       Value
1  box   Out     2017-05-13 15
2  table In      2017-04-31 65
3  box2  In      2017-05-31 65


What I have tried:

after calculateing it must be like this 
ID Name  Op_Type Date       Value Balance
2  table In      2017-04-31 65     65
1  box   Out     2017-05-13 15     50
3  box2  In      2017-05-31 65     115
Posted
Updated 21-Jun-17 23:53pm
Comments
OriginalGriff 22-Jun-17 5:18am    
"What have you tried" means just that: what have you done so far to implement this, not "what I want" - you explained that in the main question.

So what have you tried?
Where are you stuck?
What help do you need?
GTR0123 22-Jun-17 5:20am    
i have tried many thing but i always get wrong numbers thats the problem :/
OriginalGriff 22-Jun-17 5:26am    
So show us what you tried, and what it generated.
Explain where it's wrong!
GTR0123 22-Jun-17 5:28am    
select *,
sum(case when Op_Type_Id = 2 OR Op_Type_Id = 4 OR Op_Type_Id = 6 then -Value else Value end)Over(Order by [Date]) as Balance
From dbo.Reestr
GTR0123 22-Jun-17 5:22am    
just not stacked :/

1 solution

I think you can find the answer here: Running Total Sum() Problem[^]
 
Share this answer
 
Comments
GTR0123 22-Jun-17 6:06am    
this is when you have Type in diferent Columns but i have Type in 1 column dude

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