Results 1 to 5 of 5
  1. #1
    AlbertMC2 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2012
    Posts
    2

    Update with aggregate from same table


    Hi

    I have Access 2007 with a table (tblCust) containing a few fields. The 2 most important are the "ID" which is unique (PK) and a "fldValue"
    "fldValue" has numerical values in some of the records while others are blank.
    I would like to insert the maximum value of "fldValue" into the records that have no value.

    So far I have this which does not work:
    Code:
    UPDATE tblCust 
    SET tblCust.fldValue = (SELECT max(tblTemp.fldValue)
                                    from tblCust tblTemp)
    WHERE tblCust.fldValue is null;
    This gives me an "Operation must use an updateable query"

    Any ideas would be appreciated.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    In an update query, all objects involved must be updateable in order for it to work. Aggregate queries are not updateable. Even though you are not trying to update the Aggregate Query, since it is used by the query, it makes it unupdateable (I find this to be a nuisance myself!).

    You can write the results of your Aggregate Query to a temp table, and then use that temp table in your Update Query instead, and then it should work.

    Note, most of the time, if you can calculate the value you need in a query, there is no need to write the value back to the table. Doing so may actually undermine the dynamic nature of your database and could hurt database integrity (de-normalizes your database).

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    I agree, storing aggregate data is usually a bad idea. A basic principle of relational database is save raw data, calculate on reports.

    However, might be able to do what you want without temp table by using DMax domain aggregate function. http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    AlbertMC2 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2012
    Posts
    2
    Hi

    Thanks for the replies. This update will, hopefully, only occur once so I am not really worried about data integrity.
    Having said that, I assume updating the table this way will be quicker. Surely it is quicker to access the fields of one record than to access the field of all the records and find the max value?

    Can I create a temp table in the same query using a SELECT INTO or am I going to have to create 2 queries? Or is it going to be faster to just do this with VBA using the DMax or temp table?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    How else will the program know which is max if it doesn't compare all values in the field?

    One source for sql tutorial http://www.w3schools.com/sql/default.asp
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Aggregate and Non-Aggregate
    By dr4ke in forum Queries
    Replies: 6
    Last Post: 07-21-2012, 08:16 AM
  2. aggregate functions
    By gsrikanth in forum Access
    Replies: 3
    Last Post: 07-10-2012, 03:56 PM
  3. Replies: 1
    Last Post: 02-08-2012, 04:50 PM
  4. Replies: 1
    Last Post: 01-22-2011, 12:23 PM
  5. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums