Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I have one case as below.

Table & Data


CustomerName OnAcountAmt ReceivedDate CollAmt CollDate
XYZ 50,0000 01/01/2010 0 ---
XYZ 50,0000 01/01/2010 20,0000 30/01/2010
XYZ 50,0000 01/01/2010 10,0000 05/02/2010


I have required the result as below

CustomerName OnAccountAmt BalanceAmt tillCollAge
XYZ 50,0000 50,0000 30
XYZ 50,0000 30,0000 5
XYZ 50,0000 20,0000 29 [till day]


Please help me to get the above result, with the help of MS SQL Query.

Thanks,
Sunil Kumar Singh
Posted

Should be something like this:

(Assuming the table is called tblData)

SQL
SELECT
	T.CusotmerName, T.OnAccountAmt, (T.OnAccountAmt - T.ColAmt) as [Balance Amt], 
	DATEDIFF(day,T.ReceivedDate, T.ColDate) as [TilColAge]
	
FROM
	tblData as T




If that answers your question, then don't forget to mark the question as answered.
 
Share this answer
 
If I have understood your question correctly, this is an example of where the analytic function LAG() would have been very useful.
Sadly enough it doesn't exist in SQL Server (yet).

A workaround can be found here[^].
 
Share this answer
 
thanks for reply but not get the required result, as per your MS SQL, we have to calculate Balance Amount from perivous row data - next row data , similary age also.
 
Share this answer
 

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