Solved

VB.net Trying to find balance owed by customers

Posted on 2014-09-17
5
100 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:murbro
  • 3
5 Comments
 
LVL 25

Expert Comment

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

Author Comment

by:murbro
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:murbro
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:murbro
ID: 40329788
thanks for the help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

821 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