Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update query (Max(Date))

Status
Not open for further replies.

prettitoni

Programmer
Apr 22, 2004
74
US
Can someone help me write an UPDATE query that sets the Date of Table1 equal to the Max(Date) in Table2 where the tables are joined on Table1.ID = Table2.ID?
 
Hi,

If I get you right, you want to update a date field in Table1 with the Maximum date from Table2 where Table1.ID = Table2.ID

If that's true the following should help...

Code:
UPDATE Table1 
SET DateField = 
   (SELECT Max(DateField) AS MaxDate 
    FROM Table2
    WHERE Table1.ID = Table2.ID)

As this is an UPDATE I'd test on a copy of the data to ensure it's what you want.


There are two ways to write error-free programs; only the third one works.
 
That gives me "Operation must use an updateable query
 
Are you using Access 97?

There are two ways to write error-free programs; only the third one works.
 
Stumped....

I know this is a problem in Access 97 (using agregates in updates) but it's not listed as a problem in Access 2002 on MS (haven't got a copy of Access 2002 to test with though). However I've tested this in SQL Server and it works fine...


There are two ways to write error-free programs; only the third one works.
 
Give this a go...

Code:
UPDATE Table1
SET DateField = 
(DMAX("DateField","Table2","ID = " & Table1.ID))

There are two ways to write error-free programs; only the third one works.
 
Okay I tried that. The query ran but it updated all the Table1.Datefield records with blank data.
 
And something like this ?
UPDATE Table1 INNER JOIN
(SELECT Table2.ID, Max(DateField) AS MaxDate FROM Table2
GROUP BY Table2.ID) B ON Table1.ID = B.ID
SET DateField = B.MaxDate
;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top