---
title: "Data Table Sum Issue"  
description: "Data Table Sum Issue"  
author: "Andrew Deniel"  
published: 2013-06-19  
updated: 2013-06-19  
canonical: https://www.mindstick.com/forum/1200/data-table-sum-issue  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# Data Table Sum Issue

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),\
I have a situation.\
I have [datatable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) that contains the [Credit](https://www.mindstick.com/articles/85765/building-your-credit-score-when-you-re-an-uber-driver) and Debit columns like this\
Month Credit DebitSep 1422825 0Oct 0 1422825Oct 1695017.5 0Nov 0 1400000Nov 0 295018\
I want a [balance](https://www.mindstick.com/blog/300987/ways-to-improve-your-mental-focus) that should be shown like this\
Month Credit Debit BalanceSep 1422825 0Oct 0 1422825 (1422825 of Credit-1422825 of Debit)=0Oct 1695017.5 0Nov 0 1400000 Nov 0 295018 (1695017.5 of credit-1400000+295018)=0.5\
it should be shown on [crystal report](https://www.mindstick.com/articles/930/crystal-report-in-visual-studio-2010) how to do it.\
Thanks in advance.

## Replies

### Reply by Sumit Kesarwani

Hi Andrew,\
For month, used the data type integer \
**DECLARE @Temp TABLE (Month int, Credit money, Debit money, Balance money)****DECLARE @RunningTotal money****SET @RunningTotal = 0****INSERT INTO @Temp****SELECT Month, Credit, Debit, null****FROM Datatable****ORDER BY Month****UPDATE @Temp****SET @RunningTotal = Balance = @RunningTotal + Credit - Debit****FROM @Temp****SELECT * FROM @Temp****EDIT (this continues from first step):**\
If you need to display total only last record in group (month) then you can use ranking function, like...\
**;WITH Temp2 AS****(** **SELECT** ***,** **ROW_NUMBER() OVER (** **PARTITION BY Month** **ORDER BY Credit DESC, Debit DESC -- whatever order inside group you need** **) AS N** **FROM @Temp****)****SELECT** **Month,** **Credit,** **Debit,** **Balance = CASE WHEN N = 1 THEN Balance ELSE NULL END****FROM Temp2**\
I hope it working fine. \


---

Original Source: https://www.mindstick.com/forum/1200/data-table-sum-issue

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
