tags:

views:

258

answers:

2

I think this will be easy but I can't see how to do it!

I want to update the Author Table, so that the Earliest field contains the year of the earliest book. I need to use MIN somehow but how to update them all with one query?

BookTable (BookID, AuthorFK, Year)

01 34 1943
02 34 1933
03 99 1910
04 62 1990
05 99 1901

AuthorTable (AuthorID, Earliest)

34 1933
62 1990
99 1901

EDIT: HA HA WORKS PERFECTLY THANKS FOR THE SUPERQUICK ANSWER! STACKO RULES (EXCEPT FOR THE OPENID THING)

+1  A: 

update AuthorTable set Earliest = (select min(Year) from BookTable where BookTable.AuthorFK = AuthorTable.AuthorID)

Jk
+1  A: 

I would suggest doing this with a joined subquery that has grouping, such as this:

update AuthorTable set Earliest = bt.[Year] From AuthorTable as At Join ( select AuthorFK, min(Year) as [Year] from Booktable group by AuthorFK ) as bt on at.AuthorID = bt.AuthorFK

Tom Willwerth