Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends
To manage cash transactions we have table in below format

DepositWithdrawTransactionDate
1000021/02/2013
030022/02/2013
5000023/02/2013


If we do calculation at the last we get correct Balance

TotalDeposit - TotalWithdraw = Balance

6000-300=5700

But we require to display in grid view and also in Crystal report as below


DepositWithdraw Balance TransactionDate
10000100021/02/2013
030070022/02/2013
50000570023/02/2013


Deposit | Withdraw | Balance | TransactionDate
1000 0 1000 21/02/2013
0 300 700 22/02/2013
5000 0 5700 23/02/2013

Kindly help me how to query in sql server or any suggestion regarding this.
Posted
Updated 28-Feb-13 19:59pm
v2

1 solution

Hi,

Check the following Sample....

SQL
-- Table Creation Statement
IF OBJECT_ID('TempDB..#TestSample') IS NOT NULL DROP TABLE #TestSample
CREATE TABLE #TestSample(Sno INT,Opening INT,Deposit INT,Withdraw INT,Closing INT,TransactionDate DATETIME)

-- Insert Sample Values
INSERT INTO #TestSample(Sno, Opening, Deposit, Withdraw, Closing, TransactionDate)
VALUES(1, 0, 1000, 0, 0, '2013/02/21'),(2, 0, 0, 300, 0, '2013/02/22'),(3, 0, 5000, 0, 0, '2013/02/23')

-- Required Output
SELECT T.Sno, (ISNULL((SELECT SUM((Opening+Deposit-Withdraw)) 
	    FROM #TestSample 
	    WHERE Sno < T.Sno),0)) 'OpeningBalance', 
T.Deposit, T.Withdraw, 
(ISNULL((SELECT SUM((Opening+Deposit-Withdraw)) 
	    FROM #TestSample 
	    WHERE Sno < T.Sno),0) + T.Deposit - T.Withdraw) 'ClosingBalance',
T.TransactionDate
FROM #TestSample T
 
Share this answer
 
v2
Comments
R. Ganesh Kumar IT 1-Mar-13 2:13am    
Thank You
R. Ganesh Kumar IT 1-Mar-13 2:13am    
It worked well

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