Friday, April 03, 2009

SQL query join that returns only a max value in the joined table.

I have a somewhat complicated query. This query includes 4 or 5 tables. In my app, changes to the company table are logged to a company history table. I simply record the time of the update and that it was an update. When the record is created an entry is made recording the date and the type of "create.

Anyway, some records have been updated several times and I am returning search results. The user asked to have those results returned with the last updated date, which means I needed to join to that history table and return only the max(date) from records linked to the corresponding company.

The answer was to use a derived query and I found a great example here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=102255

No comments: