Solved

Use Aggregate in Update Query Access

Posted on 2012-12-27
4
482 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

770 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