I know that you should not store calculated values in a database but in this case the structure is given and I have to deal with that.
I have two tables:
Table1
with the fields (i.e. customer, product, price, count
)
Table2
with the fields (i.e. customer, product, description
)
I now need to update the field "count" in Table1 with the number of matching entries in table 2. these two tables need to be connected via "customer" and "product".
My idea was something like this:
UPDATE Table1 SET Table1.count =
(SELECT COUNT(Table2.customer)
FROM Table2
WHERE Table2.customer = Table1.customer AND Table2.product = Table1.product)
WHERE Table1.count IS NULL
But this gives an error:
Operation must be an updateable query.
I was searching this side and the web and it was proposed to use DCount function so I rewrote my code to do this:
UPDATE Table1
SET Tabl1.count = DCount( "*", "Table2", "Table2.product = "& Table1.product AND "Table2.customer = "& Table1.customer)
WHERE Table1.count IS NULL
This unfortunately always give back all entries which are existing in Table2
. So if I have 100 entries in Table2
the DCount value = 100 and not the amount of matching entries for a specific entry of Table1
(where customer and product are the same).
Can somebody point out what I am missing in that statement so that I can update the column "count" with the number of matching entries from Table2
.