Solved

VB.net Trying to find balance owed by customers

Posted on 2014-09-17
5
104 Views
Last Modified: 2014-09-18
Hi
I am trying to use the following statement to combine the total in two tables (Invoices and Payments) so that I can find the balance owed by customer. I am not sure what I have done wrong. I hope the problem is fairly obvious

            oSQL = "select"
            oSQL = oSQL & " CustomerID, sum(amount) Amount, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3]"
            oSQL = oSQL & " from"
            oSQL = oSQL & "("
            oSQL = oSQL & "select [Link ID] CustomerID, Amount * -1 As Amount, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3] from payments Inner Join Customers On [LINK ID] = [CustID]"
            oSQL = oSQL & " union all "
            oSQL = oSQL & "select invoices.customerId, [inv amt] As Amount, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3] from invoices Inner Join Customers On invoices.[CustomerID] = Customers.[CustID]"
            oSQL = oSQL & ") SubQuery"
            oSQL = oSQL & " where Amount > 0"
            oSQL = oSQL & " group by CustomerID, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3]"
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40327481
What problem are you getting? Any errors?
0
 

Author Comment

by:Murray Brown
ID: 40327488
If I run the following statement I get 1375 invoiced for CustomerID 1

select invoices.customerId, Sum([inv amt]) As Amount, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3] from invoices Inner Join Customers On invoices.[CustomerID] = Customers.[CustID]
group by CustomerID, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3]

If I run the following statement I get -1800 paid for Customer ID 1

select [Link ID], Sum(Amount) * -1 As Amount, School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3] from payments Left Join Customers On [LINK ID] = [CustID]
group by [Link ID], School, [Last Name], [First Name], [Contact No], [Child1], [Child2], [Child3]

Now using the full statement mentioned in my question I a trying to get -425 as a result but I get 1375 which is the total Invoiced and not the difference
0
 

Author Comment

by:Murray Brown
ID: 40327497
I see where I may have gone wrong. I get the balance of -425 when I take the following line out
    oSQL = oSQL & " where Amount > 0"
This line was intended to only show a client balance that is positive but it has disrupted things
I hope I am making sense
0
 
LVL 2

Accepted Solution

by:
Akilandeshwari N earned 500 total points
ID: 40327505
Your table has negative values which are excluded when you are filtering by Amount > 0.
0
 

Author Closing Comment

by:Murray Brown
ID: 40329788
thanks for the help
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question