• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

SQL Query


I have tow tables:

CustCredit and CustDebit. i wanna write a query that gives me the balance ... for example 1000 credit - 1000 debit so balance = 0

here is what i need:

In CustCredit I got 'Amount' and in CustDebit i got 'logontime' so basically SumofAmount - SumoflogonTime

can somebody help me with this plz


2 Solutions
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Assuming you have some kind of Account Number that serves as a relatable key between these two tables...

SELECT CustDebit.logontime - CustCredit.Amount AS YourBalance
FROM CustDebit
INNER JOIN CustCredit ON CustDebit.AccountNumber = CustCredit.AccountNumber

btw how does 'logontime' equate to a currency amount?
ezzadinAuthor Commented:

this is not gonna work.. I did that already and it didn't work

my logontime field is NUMBER and keep that in mind that tbldebit has more than 1000 of feilds so i have to do Sum on LogonTime and then do Sum of Debit and then DumofCredit - SumofDebit

ezzadinAuthor Commented:
yes.. and there is a unique field in those two tables called SystemID
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Unless I'm missing something, I think you'll have to create subqueries to get the sums, then link them:

Subquery #1, title "Total_Amount":
SELECT CustCredit.SystemID, Sum(CustCredit.Amount) as SumOfAmount From CustCredit;

Subquery #2, title "Total_Debit":
SELECT CustDebit.SystemID, Sum(logontime) as SumOflogontime From CustCredit;

Then a final query linking the two:
SELECT Total_Amount.SystemID, Total_Amount.SumOfAmount - Total_Debit.SumOflogontime AS Balance
FROM Total_Amount INNER JOIN Total_Credit
ON Total_Amount.SystemID = Total_Debit.SystemID;


try this:
select ((select sum(amount) from CustCredit) - (select sum(logontime) from CustDebit)) as Balance from CustCredit

ezzadinAuthor Commented:
thanks nagki

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now