0

I have this query:

UPDATE client
SET client.[client_history] = 10
FROM  [T_CLIENT] AS client
     INNER JOIN (SELECT [client_id], SUM([final_price])
            FROM [T_PURCHASE]
            GROUP BY [client_id]) AS p
     ON client.[client_id] = p.[client_id]

When i execute this query on access, i get "Syntax Error". Did you see something wrong?

Thank you

4
  • You're not using the subquery that gets joined at all. If you share a more realistic attempt, I could show you how to replace a joined subquery with a domain aggregate Commented Apr 29, 2018 at 21:06
  • I shared a realist attempt. Commented Apr 29, 2018 at 21:33
  • Eh... Explain to me what that joined subquery is doing, then. You're not using any of it's fields, why is it there? Commented Apr 29, 2018 at 21:44
  • I wanna join all list of purchases of one client, after that, i wanna save the total amount in table client column, field client_history. Commented Apr 29, 2018 at 21:50

2 Answers 2

2

You can use a DSUM to sum from a different table in an update query. Subqueries with aggregates won't work, because they're not updateable.

UPDATE t_client
SET [client_history] = DSUM("final_price", "T_PURCHASE", "client_id = " & client_id)
Sign up to request clarification or add additional context in comments.

Comments

2

Does the syntax work without FROM:

UPDATE [T_CLIENT] AS client INNER JOIN
       (SELECT [client_id], SUM([final_price])
        FROM [T_PURCHASE]
        GROUP BY [client_id]
       ) AS p
      ON client.[client_id] = p.[client_id]
    SET client.[client_history] = 10;

4 Comments

Yes, and probably he want something like SUM([final_price]) AS the_sum and then SET client.[client_history] = the_sum;, i.e. the alias is required to be able the refer to the sum. Relying on column names given automatically by Access is dangerous, because they can differ in different language versions of Access.
That won't work, since the query won't be updatable. You can usually replace INNER JOIN and SUM with DSUM, but I'm a little unclear on the question since the joined subquery doesn't get used
Yes, i get an error: "Operation must use an updateable query" Anyone knows?
@Gordon Linoff This question has similar issue. It says Operation must use an updateable query. Can you please have a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.