Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A SQL QUERY ?

Status
Not open for further replies.

vcavusoglu

Programmer
Joined
Apr 26, 2001
Messages
41
Location
TR
I HAVE GOT A TABLE AS

NO DEBT CLAIM
1 100 0
2 0 40
3 50 15

AND A WANT THIS RESULT QUERY
DEBT CLAIM REMAINDER
100 0 100
0 40 60
0 15 95


REMAINDER = PREVIOUS REMAINDER + LAST DEPT - LAST CLAIM

EXAMPLES

100= 0 + 100 - 0
60 = 100 + 0 - 40
95 = 60 + 50 - 15

THANKS
 
I don't know of a way to do this through straight SQL. Any answer I can come up with involves a cursor, or using client-side code to loop through the result set.

Chip H.
 

Here is a SQL solution. It involves a correlated subquery. You will need to change the sample to fit your table and column names. I intruduced a column you didn't have in your sample - DebtID. If your table only tracks one set of numbers and you want to summarize accross the entire table this column won't be needed. If you have some sort of breakout by DebtID or other identifier, you'll need a similar colummn to separate the data by the identifier.

Select
DebtAmt, ClaimAmt,
RemainderAmt=
(Select isnull(Sum(DebtAmt-ClaimAmt),0)
From tblDebt
Where DebtID=Curr.DebtID
And RecNo<Curr.RecNo)+
isnull(DebtAmt,0)-isnull(ClaimAmt,0)
From tblDebt As Curr

Hope this helps get you started. Let me know if you have any questions about the query. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top