I have two tables, call them Table1 and Table2.
Table1 has 10 fields. The first two (the only two which matter to this problem) are
CNP: Primary Key, Short Text field
Money: Number field
Table2 has 3 fields.
ID: Primary Key
CNP: Short Text
Money: Number field
I want the value of Table1.Money corresponding to each record by CNP to be equal the sum of all the Table2.Money values for the said CNP value. So say Table1 has 3 records and Table2 has 5 records as follows:
Table2
1; 20; 200
2; 21; 300
3; 20; 300
4; 20; 100
5; 21; 500
Table1
20; ?
21; ?
22; ?
I want the "?" in Table1 to equal (200+300+100) for CNP 20, (300+500) for CNP 21, and 0 for CNP 22 (as there are no records in Table2 for it).
The question is how can I achieve this? One way I can envisage to do this is (1) to create a query - call it Query1 - which takes Table2 and sums up all duplicates, such that Query1 prints:
20; 600
21; 800
(2) Then I relate this Query1 with Table1 based on the CNP field in a 1-To-1 Relationship, and (3) using an update Query, call it Query 2, update the Money Field of Table1 with the Money Field in Query1 IF Query1.CNP = Table1.CNP and otherwise update the Money Field in Table1 with 0.
The issue is that even if this is the right way to do this, I'm not sure how to execute step (1) of the plan. Is there another way to do this? If there isn't or if the way I proposed is the optimal way to achieve this goal, how can I create a query which takes a Table, and sums all the duplicates, such that N records having CNP value of X are transformed into 1 record having CNP value X? Thanks!