473,597 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Table Query using Sum Function

I am trying to query 3 tables all related by Clinet_ID. The Clients
table, Monthly_Expense s 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_Expense s.Past_Due_Amou nt, 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_Expense s.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_Inc ome.Amount) AS SumOfAmount,
Sum(Monthly_Exp enses.Amount) AS SumOfAmount1,
Sum(Monthly_Exp enses.Past_Due_ Amount) AS SumOfPast_Due_A mount
FROM (Clients INNER JOIN Monthly_Expense s ON Clients.Client_ ID =
Monthly_Expense s.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_Expense s 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 15048
Scott Cannon <sm********@hot mail.com> wrote:
I am trying to query 3 tables all related by Clinet_ID. The Clients
table, Monthly_Expense s 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_Expense s.Past_Due_Amou nt, 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_Expense s.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|Inco me
1| 1| 1
1| 1| 2

If you sum grouped by ID, you'd get

ID|Expense|Inco me
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
1423
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 The inventory table basically has a part number, and all of the part details:
3
3485
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 detail and Billing) This query helps to create our invoice forms. My problem:
27
2724
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 would appreciate any help you can give. This is a database to make reservations I am trying to run a query against two tables. The first table (families) has the following fields Family Neighborhood Street nmbsnglebedsavail
0
2007
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 defined "attributes" or "dimensions" that in turn connect to the actual product table via a many-many (using a linking table). In each linking table there is a combination of the "dimension", the productID, and the "fact" that is stored against the...
2
1963
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 started working on the forms and encountered an issue that has me baffeled. ***Table Info**** Employees have skill sets and it is a M:M relationship Employees have training and it is a M:M relationship Skill Sets have trainings and it is a M:M...
3
4403
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) Laptop(code, model, speed, ram, hd, screen, price) Printer(code, model, color, type, price)
112
6890
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 of tables. there are 58 tables that are all nearly identical to the one shown... each table represents a txt file that is imported every night to our server and each txt file is a store. These files hold the info about the terminals..
4
5732
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 HAVING (((Count(*))=1)); I want to create a make table query.
0
1084
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 different instances of employee number: A Run Log which holds Driver# and Passenger#
0
7885
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6686
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.