473,396 Members | 2,092 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,396 software developers and data experts.

Update using 2 tables and a sum

Ahhh... I can't figure this out, can someone help?

I'm using Access 2003.

I have 2 tables here:

One is for each person, called tblPartners.

One is for each distribution by a partner, called tblDistributions

In tblPartners there is a field called ClosingBalance.

What I want to do is UPDATE ClosingBalance to equal the sum of
distributions by that partner for a period.

So what I originally did was try

UPDATE tblPartners LEFT JOIN tblDistributions
ON tblPartners.PartnerID = tblDistributions.PartnerID
AND tblPartners.Period= tblDistributions.inPeriod
SET tblPartners.ClosingBalance = SUM(tblDistributions.DistribAmt)

But this gives the error "You tried to execute a query that does not
include the specified expression 'ClosingBalance' as part of an
aggregate function."

I also tried:
UPDATE tblPartners
SET ClosingBalance = TotalDistribs
FROM tblPartners INNER JOIN tblDistributions
(SELECT SUM(DistribAmt) AS TotalDistribs, PartnerID, inPeriod
FROM tblDistributions GROUP BY PartnerID, inPeriod)
ON tblPartners.PartnerID = tblDistributions.ParnterID AND
tblPartners.Period = tblDistributions.inPeriod

But gives a syntax error.

Jul 17 '07 #1
2 2754
ki*********@gmail.com wrote:
Ahhh... I can't figure this out, can someone help?

I'm using Access 2003.

I have 2 tables here:

One is for each person, called tblPartners.

One is for each distribution by a partner, called tblDistributions

In tblPartners there is a field called ClosingBalance.

What I want to do is UPDATE ClosingBalance to equal the sum of
distributions by that partner for a period.

So what I originally did was try

UPDATE tblPartners LEFT JOIN tblDistributions
ON tblPartners.PartnerID = tblDistributions.PartnerID
AND tblPartners.Period= tblDistributions.inPeriod
SET tblPartners.ClosingBalance = SUM(tblDistributions.DistribAmt)

But this gives the error "You tried to execute a query that does not
include the specified expression 'ClosingBalance' as part of an
aggregate function."

I also tried:
UPDATE tblPartners
SET ClosingBalance = TotalDistribs
FROM tblPartners INNER JOIN tblDistributions
(SELECT SUM(DistribAmt) AS TotalDistribs, PartnerID, inPeriod
FROM tblDistributions GROUP BY PartnerID, inPeriod)
ON tblPartners.PartnerID = tblDistributions.ParnterID AND
tblPartners.Period = tblDistributions.inPeriod

But gives a syntax error.
Why don't you create a Totals query on tblDistributions and use that
Totals query to link to tblPartners to get the value to update.

I suppose you could use Dsum() to get the closing balance as well...make
sure you filter it correctly.
Jul 17 '07 #2
First create a query that groups partners and sums the distributions:

SELECT tblDistributions.PartnerID , Sum(tblDistributions.DistribAmt)
AS TheSum
FROM tblDistributions
GROUP BY tblDistributions.PartnerID ;

Later create an update query that joins the first query with the
partners table.

But, storing a sum in a parent table maybe isn't such a good idea,
what happens when the child records change? who's in charge of
updating the closing balance to keep it in sync?

Kind regards
C.
On Jul 17, 12:00 pm, kidforto...@gmail.com wrote:
Ahhh... I can't figure this out, can someone help?

I'm using Access 2003.

I have 2 tables here:

One is for each person, called tblPartners.

One is for each distribution by a partner, called tblDistributions

In tblPartners there is a field called ClosingBalance.

What I want to do is UPDATE ClosingBalance to equal the sum of
distributions by that partner for a period.

So what I originally did was try

UPDATE tblPartners LEFT JOIN tblDistributions
ON tblPartners.PartnerID = tblDistributions.PartnerID
AND tblPartners.Period= tblDistributions.inPeriod
SET tblPartners.ClosingBalance = SUM(tblDistributions.DistribAmt)

But this gives the error "You tried to execute a query that does not
include the specified expression 'ClosingBalance' as part of an
aggregate function."

I also tried:
UPDATE tblPartners
SET ClosingBalance = TotalDistribs
FROM tblPartners INNER JOIN tblDistributions
(SELECT SUM(DistribAmt) AS TotalDistribs, PartnerID, inPeriod
FROM tblDistributions GROUP BY PartnerID, inPeriod)
ON tblPartners.PartnerID = tblDistributions.ParnterID AND
tblPartners.Period = tblDistributions.inPeriod

But gives a syntax error.

Jul 18 '07 #3

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

Similar topics

5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
2
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
7
by: jaYPee | last post by:
I am having a problem on how to update view from sql server 2000 database. I have tried dataset and sql dataadapter but no luck. I'm using this view in datagrid and i want to update the datagrid if...
15
by: graham | last post by:
Hi all, <bitching and moaning section> I am asking for any help I can get here... I am at the end of my tether... I don;t consider myself a genius in any way whatsoever, but I do believe I have...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
15
by: Scotty | last post by:
I like to have a good insert, update and delete code The code below sometimes workl ok sometimes doesnt work, what i am doing wrong?? Sub SaveAny() Dim command_builder As New...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.