?
Solved

VB.net Trying to find balance owed by customers

Posted on 2014-09-17
5
Medium Priority
?
105 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 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