# SQL Server 2000 ; have 10 rows of DEBIT + CREDIT columns, can SQL tally up the balances for each account?

### MS SQL Server Question

hi, this is my table. is it possible do work out the balance via SQL, im currently doing it in .net.
Account   Date                         Debit        Credit      Balance
10139      2007-08-31       2025.91      0.0      0.0
10139      2007-08-31       0.0      3620.11      0.0
10139      2007-09-30       4631.52      0.0      0.0
10139      2007-09-30       0.0      11336.71      0.0
10139      2007-09-30       0.0      14.8801      0.0
12211      2007-08-31       1352.76      0.0      0.0
12211      2007-08-31       0.0      3872.5      0.0

e.g. will be
Account   Date                         Debit        Credit      Balance
10139      2007-08-31       205      0.0      -205
10139      2007-08-31       0.0      305      +100
10139      2007-09-30       0.0      15      +115
12211      2007-08-31       135      0.0      -135
12211      2007-08-31       0.0      325      +190

Default Text

The member who asked this question verified this comment provided the solution that solved their problem.

Posted on 2007-11-30 at 04:51:06ID: 20381365

This should do it

``````CREATE TABLE
#mutations
(
Account INTEGER,
Date DATETIME,
Debit DECIMAL(9,2),
Credit DECIMAL(9,2)
)

INSERT INTO #mutations VALUES (10139, '2007-08-31', 2025.91, 0.0)
INSERT INTO #mutations VALUES (10139, '2007-08-31', 0.0, 3620.11)
INSERT INTO #mutations VALUES (10139, '2007-09-30', 4631.52, 0.0)
INSERT INTO #mutations VALUES (10139, '2007-09-30', 0.0, 11336.71)
INSERT INTO #mutations VALUES (10139, '2007-09-30', 0.0, 14.8801)
INSERT INTO #mutations VALUES (12211, '2007-08-31', 1352.76, 0.0)
INSERT INTO #mutations VALUES (12211, '2007-08-31', 0.0, 3872.5)

/*
e.g. will be
Account   Date                         Debit        Credit      Balance
10139      2007-08-31       205      0.0      -205
10139      2007-08-31       0.0      305      +100
10139      2007-09-30       0.0      15      +115
12211      2007-08-31       135      0.0      -135
12211      2007-08-31       0.0      325      +190
*/

SELECT
account,
date,
SUM(Debit) AS Debit,
- SUM(Credit) AS Credit,
(SELECT ISNULL(SUM(m2.Debit),0) - ISNULL(SUM(m2.Credit),0) FROM #mutations m2 WHERE m1.account = m2.account AND m2.date <= m1.date AND

CASE WHEN m2.debit <> 0 THEN 0 ELSE 1 END <= CASE WHEN m1.debit <> 0 THEN 0 ELSE 1 END

) AS Balance
FROM
#mutations m1
GROUP BY
account,
date,
CASE WHEN debit <> 0 THEN 0 ELSE 1 END
ORDER BY
account,
date,
CASE WHEN debit <> 0 THEN 0 ELSE 1 END

DROP TABLE #mutations```
```
```1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
```
This content is available to Experts Exchange members

• 4,169,477 solved questions
• 3,805 articles & videos
• 15,413 tech experts

## Related Questions

See More MS SQL Server Solutions

## Related Articles

See More MS SQL Server Articles

## Trusted by Thousands of Top Companies

• CVS
• IBM
• Boeing
• US Army
• Kaiser
• RalphLauren
• Xerox
• Cox
• BlueCross
• Accenture
• Sony
• Toyota
• Allstate
• Att
• TomTom
• BBC
• Raytheon
• Wells Fargo
• US Navy
• Direct TV
• FootLocker
• Nikon
• BritishCouncil
• TS Army
201503-LO-Qu-039

Experts Exchange members save,
on average, 2.5 hours per week.

- 2014 EE Annual Survey

## Related Questions We have nearly 4 million solutions here.

See More MS SQL Server Solutions

• ## Solve

Experts Exchange is the tech professional’s trusted, on-demand resource for solving difficult problems, making informed decisions, and delivering excellent solutions.

• ## Learn

With unparalleled access to technical experts, verified real-world solutions, and diverse educational content, Experts Exchange enables personalized development of technology skills.

• ## Network

Experts Exchange gives you the professional exposure and valued relationships key to building the career you want.