Link to home
Start Free TrialLog in
Avatar of petekipe
petekipeFlag for United States of America

asked on

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,

Pete
Avatar of Anuroopsundd
Anuroopsundd
Flag of India image

http://www.techonthenet.com/access/queries/update2_2007.php

use the max functionfor getting highest value
Avatar of petekipe

ASKER

Max function is my problem -- I don't know how to incorporate it.  Can you provide an example?
Avatar of peter57r
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.
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.
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.
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],'') <>''
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.
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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)
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!

Pete