Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-03-15 : 07:54:45
hoshang writes "i have table with trid,pay_in,pay_out

what i want is how to calculate the current_balance at each record,

e.g .. i want to select trid,pay_in,pay_out, (Some kind of calculation to get the current_balance ) as current_balance

i want to have the following result

itd : payin : payout : current_balance

1 : 2000 : 0 : 2000
2 : 0 : 1000 : 1000
3 : 50 : 0 : 1050
---

I realy need your help

thanks"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-15 : 07:59:09
[code]select itd, payin, payout,
(select sum(payin - payout) from yourtable x where x.itd <= t.itd) as current_balance
from yourtable t[/code]


----------------------------------
'KH'


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-15 : 08:08:42
Where do you want to show the data?
I think you need to make use of Reports and its Running Total feature. Doing this in SQL, can lead to performance problems if there are millions of records in the table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -