简体   繁体   中英

How to SUM two columns and display the result in the third column by subtracting their aggregates SQL & C#?

I have three columns in my database table, namely Debit , Credit and Balance . I need to calculate the total balance after summing up Total Debit, and total Credit, and show the result in a textbox that is read only.

The code of query is here

SELECT *
FROM TableName SUM(Debit) MINUS SUM(Credit) AS TotalBalance

Now I have to show Total Balance along with Total Debit And Total Credit. Query runs fine but with no apparent results.

The query that you want is presumably:

SELECT SUM(Debit) - SUM(Credit) AS TotalBalance
FROM TableName;

This returns one row with one value that is the difference.

If you're using SQL server , use the below script.

    SELECT SUM(debit) TotalDebit,SUM(credit) TotalCredit
       ,SUM(Debit)-SUM(credit) TotalBalance
     FROM TableName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM