|
I am trying to query 3 tables all related by Clinet_ID. The Clients
table, Monthly_Expenses table and Monthly_Income table. Each client
can have 0>M instances of expenses, past due expenses, and income so I
am using the SUM aggregate function to get the totals for the
Monthly_Epenses.Amount, Monthly_Expenses.Past_Due_Amount, and
Monthly_Income.Amount.
Then problem I am having and cannot seem to resolve is that the
Monthly_Income is not toalling correctly. The Monthly_Expenses.Amount
calculates fine as well as the Past_Due_Amount. The income is coming
out at some multiple of the actual amount. Client_ID 9 has
Monthly_Income.Amount of $500 and the query result is $4000.
Should I do multiple separate queries and then query the results table
to get the Monthly Income snapshot I am looking for?
Here's the SQL for the query as MS Access built it:
SELECT Clients.Client_ID, Sum(Monthly_Income.Amount) AS SumOfAmount,
Sum(Monthly_Expenses.Amount) AS SumOfAmount1,
Sum(Monthly_Expenses.Past_Due_Amount) AS SumOfPast_Due_Amount
FROM (Clients INNER JOIN Monthly_Expenses ON Clients.Client_ID =
Monthly_Expenses.Client_ID) INNER JOIN Monthly_Income ON
Clients.Client_ID = Monthly_Income.Client_ID
GROUP BY Clients.Client_ID;
Here's one I wrote myself that is aliased for readablility and returns
the same results and the MS Access query:
SELECT C.Client_ID, Sum(MI.Amount) AS [Monthly Income], Sum(ME.Amount)
AS [Monthly Expenses], sum(ME.Past_Due_Amount) AS Past_Due_Amount,
(Sum(MI.Amount)-Sum(ME.Amount)) AS [Monthly Net Income]
FROM Clients AS C, Monthly_Income AS MI, Monthly_Expenses AS ME
WHERE C.Client_ID = MI.Client_ID AND C.Client_ID = ME.Client_ID
GROUP BY C.Client_ID; |