SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Financial Balance
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

FernandoLorival
Starting Member

Canada
19 Posts

Posted - 09/27/2013 :  12:48:10  Show Profile  Reply with Quote
Hi all,
I have a financial table that holds financial transaction, one of the fields is Balance. When a transaction is posted that field is populated with an amount, when the client pays that amount or a fraction of the amount, another transaction is created (row) that will deduct from the initial balance. This happens until the balance is 0.
Now, I need a report that shows only my open balances, basically the last transaction posted against the client.

Here's my code example:

DECLARE @TEMP1 TABLE (ClientNo INT, TransactionID INT, TransAmount INT, Balance INT);

INSERT @TEMP1
VALUES (1, 1, 123, 123),
(1, 2, -123, 0),
(2, 1, 500, 500),
(3, 1, 45000, 45000),
(4, 1, 45, 45),
(5, 1, -234, -234),
(5, 2, 123, -111),
(6, 1, 234234, 234234)


In this case I would like to get the information for Client 2,3,4,5 and 6.
NOTE: For client 5 I only want to show the last line, open balance -111.


Thank you all for your time.


James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 09/27/2013 :  12:55:29  Show Profile  Reply with Quote
SELECT * FROM
(
	SELECT *,
		ROW_NUMBER() OVER(PARTITION BY clientNo ORDER BY TRansactionId DESC) AS RN
	FROM
		@Temp1
)s WHERE Balance <> 0 AND RN = 1
Go to Top of Page

FernandoLorival
Starting Member

Canada
19 Posts

Posted - 09/27/2013 :  13:01:14  Show Profile  Reply with Quote
Thank you James,
That works!
:)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000