Update table A from TOP in table B

Given table A as follows:

pk_a   expiration_date
1        1/15/2012
2        1/15/2012
3        2/12/2012

and table B as follows:

pk_b   fk_a            date_expires
1        1                1/15/2012
2        1                1/15/2013
3        2                1/15/2012
4        2                1/15/2013
5        3                2/12/2011
6        3                2/12/2012
7        3                2/12/2013

How can I write an update query in Access 2003 SQL that will update A.expiration_date with B.date_expires from the highest B.pk_b for the join between A.pk_a and B.fk_a?

Thanks in advance,

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


use the max functionfor getting highest value
petekipeOwnerAuthor Commented:
Max function is my problem -- I don't know how to incorporate it.  Can you provide an example?
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Update A set A.expiration_date = Dmax("expiration_date", "B", "fk_a=" & pk_a)

The above assumes pk_a is numeric.

If it is text then you would need..

Update A set A.expiration_date = Dmax("expiration_date", "B", "fk_a='" & pk_a "'")

You cannot run an update query in Access if it includes any simple aggregate function (max(), min() , avg() etc) anywher in its structure.
You CAN use the domain aggregates DMax() DMin() etc as in the solution above.
petekipeOwnerAuthor Commented:
Neither of the solutions in the stackoverflow link work in Access 2003, nor does the solution in access-programmers.co.uk.  The following looks logically like what I want -- I've changed the code to the real table and column names:

UPDATE     Station A
SET             expiration_date =
                      (SELECT   MAX([date_expires])
                       FROM      License B
                       WHERE   A.pk_station = B.fk_station)

But I'm getting the error, "Operation must use an updateable query."  Any ideas?
See my post immediately before yours.
petekipeOwnerAuthor Commented:
peter57r, you're really close -- but the code sets Station.expiration_date to the maximum expiration date of any of its joined rows from License.  Referring to the tables given in my original post, I need to set A.expiration_date to the B.date_expires of the highest B.pk_b for the join of A.pk_a = B.fk_a.  i.e, the date I want isn't necessarily the maximum date in B for the join, it should be the last-entered date (having the highest B.pk_b for the join).  Does that make sense?
The code I posted does what you are asking.
Post the sql you are using.
petekipeOwnerAuthor Commented:
UPDATE Station
SET    Station.expiration_date = Dmax("date_expires", "License", "fk_station=" & pk_station)

What in the code causes date_expires to be chosen from the highest License pk related by foreign key to a particular Station pk?
This bit of the DMax does the matching...

 "fk_station=" & pk_station)

This Update command definitely updates table Station with the max date_expires for the matching keys.

Can you confirm that both Station.expiration_date and date_expires are defined as datetime fields.
Your Query:

UPDATE     Station A
SET             expiration_date =
                      (SELECT   MAX([date_expires])
                       FROM      License B
                       WHERE   A.pk_station = B.fk_station)

First option: Run the sub query individually and check whether its giving records or not.

SELECT   MAX([date_expires])  FROM      License B   WHERE   A.pk_station = B.fk_station

if you still face the error check there might be null values inside "date_expires" field

if it contains null or blank values filter null value records by using

SELECT   MAX([date_expires])  FROM      License B   WHERE   A.pk_station = B.fk_station and isnull([date_expires],'') <>''
petekipeOwnerAuthor Commented:
keyu:  Access 2003 does not allow an update query to contain a subquery containing a MAX or TOP function.

peter57r:  Both fields are defined as datetime fields.  I don't think I made the point clear in my ID: 37815355 post:  I don't want the maximum date value -- I want the date value from the most recent License row, which is the row having the highest pk_license among the joined rows.
I see.

Then it gets more complicated.  I think this is what you want (subject to my typos!)

Update A set A.expiration_date = DLookup("expiration_date", "B", "pk-b =" & Dmax("pk-B", "B", "fk_a= " & pk_a))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UPDATE     Station A
SET             expiration_date =
                      (SELECT   top 1 date_expires
                       FROM      License B
                       WHERE   A.pk_station = B.fk_station order by date_expires desc)
petekipeOwnerAuthor Commented:
peter57r:  Bingo!  For the record, following is my final query:

Update Station
SET       Station.expiration_date = DLookup("date_expires", "License", "pk_license =" & Dmax("pk_license", "License", "fk_station= " & pk_station))

Note:  Out of roughly 18000 Station rows, only about 1000 had corresponding License rows. According to MS's docs on DLookup/Dmax, if the conditions are not met, null is returned, which caused 17000 rows not to be updated because of type conversion errors.  Ideally, the update could have been accomplished via a right join, to eliminate unlicensed stations from even being considered, but as was pointed out early in this thread, Access doesn't allow an aggregation function in an update query.

Thank you VERY much for your insightful help, peter57r!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.