Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use Aggregate in Update Query Access

Posted on 2012-12-27
4
Medium Priority
?
534 Views
Last Modified: 2012-12-27
I am trying to update an invoice total on an invoice table from the lineitem totals on an invoice details table.  I'm finding using an aggregate function within an update query is challenging.

I'm adding these total fields to make it easier for management to gather stats in our small firm.

Here is the SQL that I think should work but it not working and throwing a: "Syntax Error (missing operator) in query expression 't.total from tblPO p inner join..."

UPDATE P
SET p.PO_total = t.total FROM tblPO p 
INNER JOIN
     (SELECT tblPO_Details.PO_ID, SUM(tblPO_Details.subtotal) as total
      FROM tblPO_Details
      GROUP BY tblPO_Details.PO_ID) as t
    ON t.PO_ID = p.PO_ID

Open in new window


Any ideas?
0
Comment
Question by:ClaudeWalker
  • 2
4 Comments
 

Author Comment

by:ClaudeWalker
ID: 38724824
I also tried this and it throws "must use updatable query"

UPDATE tblPO 
INNER JOIN
     (SELECT tblPO_Details.PO_ID, SUM(tblPO_Details.subtotal) as total
      FROM tblPO_Details
      GROUP BY tblPO_Details.PO_ID) as t
    ON t.PO_ID = tblPO.PO_ID
SET tblPO.PO_total = t.total 

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 38724859
As far as I know, you can't use a subquery in your UPDATE statement like this in Access.

A workaround is to use your aggregate query to make a temporary table (turn it into a make table query) and use that table as the source for your UPDATE.
0
 

Author Closing Comment

by:ClaudeWalker
ID: 38724905
That's the only thing that worked :)
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38724941
UPDATE tblPO AS a SET a.po_total = DSum("subtotal","tblPO_Details","po_id = " & [a].[po_id]);
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question