473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Multiple Table Query using Sum Function

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;
Nov 12 '05 #1
2 15011
Scott Cannon <sm********@hotmail.com> wrote:
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?


Yes, you should. The joins are performed first, then the sum function is
run over all grouped records. In your case some fields are summed
several times. Remove the GROUP BY clause and look at the ungrouped
query to see what I mean.

If there are three tables tblA, tblB and tblC joined on ID like this

tblA:

ID
-
1
2

tblB:

ID|Expense
1|1
tblC:

ID|Income
1|1
1|2
A query would return this

ID|Expense|Income
1| 1| 1
1| 1| 2

If you sum grouped by ID, you'd get

ID|Expense|Income
1| 2| 3

which is wrong for Expense.

Sum the two tables separetely and then join the queries to your main
table.

HTH,
Chris
Nov 12 '05 #2
Chris,

That did the trick. I ended up summing the Income and Expense data in
their own separate queries, then used them along with the original
tables to create the financial snapshot I was looking for with only
the Net Income: field as a calculated field using the Income and
Expense query totals in the expression.

Using the Nz function setting any nulls to zero also helped to make a
perfect query result set.

Thanks.

Scott
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeff Homan | last post by:
Hello all, I have a problem with a query in Access 97, and I'll try to give you all of the details to see if you have any idea if it's even possible: I have 2 tables: Inventory and Pricing ...
3
by: Beejer | last post by:
I'm currently having a hard time getting my expression to run in my billing query. My setup: MS Access 2003 Qry Billing (includes 6 tables such as Client, Price list, Formula, Order, Order...
27
by: spminba | last post by:
First, I want to say I have used this board to resolve many issues that I have had with access and I thank everyone who contributes. I have attempted to resolve my issue myself, but I am stumped and...
0
by: The Frog | last post by:
Hello Everyone, I have been asked to try and create a single SQL query to retrieve product information from a database. The way that data is arranged is that in some tables there are user...
2
by: Leventcos21 | last post by:
Hi all, I have been stuck on this issue for weeks now and can not find a solution, so I hoping that someone can help me out. I am setting up a training database. I have setup the structure and...
3
by: binita2908 | last post by:
Hi guys , I am quite a baby to sql , pls help me out in this . My database scheme consists of four relations: Product(maker, model, type) PC(code, model, speed, ram, hd, cd, price)...
112
by: mbatestblrock | last post by:
I really need some direction writing a query here. I provided a screen cap so you may catch the drift. http://www.mykesdesigns.com/tblrock1/access/terminfo.jpg as you may see there are a ton...
4
by: sam15024 | last post by:
Hi I am newbie, can you please help me to create this query through vba SELECT .Field1, .Field2, .Field3, .Field4, .Field5 FROM 010710_pri GROUP BY .Field1, .Field2, .Field3, .Field4, .Field5...
0
by: Jeff Miller | last post by:
(No code of any sort follows) Scenario: Need to generate an annual performance report for any given employee. Personnel are keyed by an employee number (#) Two tables each which handle two...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.