~Sum existing values with values from another table 
Author Message
 ~Sum existing values with values from another table

I want to add values from one column of table B (obtained by a SELECT query)
to existing values in table A where the primary keys are equal.

Put another way, I usually do an INSERT INTO statement to add the records
from table B (obtained by a SELECT query) into table A.  But if records
already exist in table A with the same primary key as the records I want to
add from table B, the primary keys will conflict.  In this case, I want to
add the values of one column of the records in table B to the same column
for the corresponding records in table A (with the same primary key). (whew,
I hope that made sense).  It's kinda like this: Insert the records from
table B into table A, but if they already exist in table A, then add the
values from table A and table B together.

I can't get any kind of UPDATE query to work.  I have also searched DejaNews
with no luck.

Does anyone know how to do this in SQL (or even straight design view)?

Many TIA :-)



Fri, 20 Jul 2001 03:00:00 GMT  
 ~Sum existing values with values from another table
Since you need to add values to existing matches, it's best to do this in
two steps:

Step1:
UPDATE TableA INNER JOIN TableB ON (TableA.PKey = TableB.PKey)
SET TableA.SomeField = TableA.SomeField + TableB.SomeField;

Step2:
INSERT INTO TableA
SELECT TableB.*
FROM TableB LEFT JOIN TableA ON (TableB.PKey = TableA.PKey)
WHERE TableA.PKey IS NULL;

--
John Viescas
author, "Running Microsoft Access 97"
http://www.amazon.com/exec/obidos/ISBN=1572313234/

Quote:

>I want to add values from one column of table B (obtained by a SELECT
query)
>to existing values in table A where the primary keys are equal.

>Put another way, I usually do an INSERT INTO statement to add the records
>from table B (obtained by a SELECT query) into table A.  But if records
>already exist in table A with the same primary key as the records I want to
>add from table B, the primary keys will conflict.  In this case, I want to
>add the values of one column of the records in table B to the same column
>for the corresponding records in table A (with the same primary key).
(whew,
>I hope that made sense).  It's kinda like this: Insert the records from
>table B into table A, but if they already exist in table A, then add the
>values from table A and table B together.

>I can't get any kind of UPDATE query to work.  I have also searched
DejaNews
>with no luck.

>Does anyone know how to do this in SQL (or even straight design view)?

>Many TIA :-)



Fri, 20 Jul 2001 03:00:00 GMT  
 ~Sum existing values with values from another table
Thanx for the reply! It worked, but...

This works great if the primary key is a single field.  What would the
syntax be if the primary key contained 3 fields?

Many, many TIA

Quote:

>Since you need to add values to existing matches, it's best to do this in
>two steps:

>Step1:
>UPDATE TableA INNER JOIN TableB ON (TableA.PKey = TableB.PKey)
>SET TableA.SomeField = TableA.SomeField + TableB.SomeField;

>Step2:
>INSERT INTO TableA
>SELECT TableB.*
>FROM TableB LEFT JOIN TableA ON (TableB.PKey = TableA.PKey)
>WHERE TableA.PKey IS NULL;

>--
>John Viescas
>author, "Running Microsoft Access 97"
>http://www.amazon.com/exec/obidos/ISBN=1572313234/

>>I want to add values from one column of table B (obtained by a SELECT
>query)
>>to existing values in table A where the primary keys are equal.

>>Put another way, I usually do an INSERT INTO statement to add the records
>>from table B (obtained by a SELECT query) into table A.  But if records
>>already exist in table A with the same primary key as the records I want
to
>>add from table B, the primary keys will conflict.  In this case, I want to
>>add the values of one column of the records in table B to the same column
>>for the corresponding records in table A (with the same primary key).
>(whew,
>>I hope that made sense).  It's kinda like this: Insert the records from
>>table B into table A, but if they already exist in table A, then add the
>>values from table A and table B together.

>>I can't get any kind of UPDATE query to work.  I have also searched
>DejaNews
>>with no luck.

>>Does anyone know how to do this in SQL (or even straight design view)?

>>Many TIA :-)



Fri, 20 Jul 2001 03:00:00 GMT  
 ~Sum existing values with values from another table
Michael-

All you need to do is add all three comparisons to the respective ON
clauses.

--
John Viescas
author, "Running Microsoft Access 97"
http://www.amazon.com/exec/obidos/ISBN=1572313234/

Quote:

>Thanx for the reply! It worked, but...

>This works great if the primary key is a single field.  What would the
>syntax be if the primary key contained 3 fields?

>Many, many TIA


>>Since you need to add values to existing matches, it's best to do this in
>>two steps:

>>Step1:
>>UPDATE TableA INNER JOIN TableB ON (TableA.PKey = TableB.PKey)
>>SET TableA.SomeField = TableA.SomeField + TableB.SomeField;

>>Step2:
>>INSERT INTO TableA
>>SELECT TableB.*
>>FROM TableB LEFT JOIN TableA ON (TableB.PKey = TableA.PKey)
>>WHERE TableA.PKey IS NULL;

>>--
>>John Viescas
>>author, "Running Microsoft Access 97"
>>http://www.amazon.com/exec/obidos/ISBN=1572313234/

>>>I want to add values from one column of table B (obtained by a SELECT
>>query)
>>>to existing values in table A where the primary keys are equal.

>>>Put another way, I usually do an INSERT INTO statement to add the records
>>>from table B (obtained by a SELECT query) into table A.  But if records
>>>already exist in table A with the same primary key as the records I want
>to
>>>add from table B, the primary keys will conflict.  In this case, I want
to
>>>add the values of one column of the records in table B to the same column
>>>for the corresponding records in table A (with the same primary key).
>>(whew,
>>>I hope that made sense).  It's kinda like this: Insert the records from
>>>table B into table A, but if they already exist in table A, then add the
>>>values from table A and table B together.

>>>I can't get any kind of UPDATE query to work.  I have also searched
>>DejaNews
>>>with no luck.

>>>Does anyone know how to do this in SQL (or even straight design view)?

>>>Many TIA :-)



Fri, 20 Jul 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ~Sum existing values with values from another table

2. Adding required fields with default values to existing tables

3. Adding required fields with default values to existing tables

4. Adding Property Values to an existing table

5. Value from a tbl to calc value in another table

6. Sum Recordset Values

7. Summing values in records of a subform

8. Access 97 sum decimal values grief...

9. Macro to assign value to and sum up check box forms

10. Sum values in a column in ListView

11. sum values in datagrid

12. Please help:sum values in msflexgrid rows

 

 
Powered by phpBB® Forum Software