I currently have a query that works, but I'm curious to know if there is a better way to do this.What I have basically, is a table with values with an effective date for each value. The query needs to choose the value with the most recent effective date, which does not come after the date on the associated record.Here is an example:Lookup values:Incident - Value - DateA..........5.......1/1/2004A..........8.......1/1/2005A..........4.......1/1/2006B..........1.......2/1/2004B..........3.......2/1/2005B..........2.......2/1/2006C..........20......6/1/2004C..........50......6/1/2005C..........36......6/1/2006
Now the data that this would be appended to would be along the following lines:Incidents:Incident - DateA..........3/24/2005B..........6/08/2006C..........9/09/2008A..........1/01/2003
So, the joined data should look like this:Incident - Date --- Incident - Value - DateA........3/24/2005..A..........8.......1/1/2005B........6/08/2006..B..........2.......2/1/2006C........9/09/2008..C..........36......6/1/2006A........1/01/2003..NULL
What I'm currently doing is using a subquery in the join, like this:SELECT *FROM Incidents i LEFT JOIN Elements e ON e.incident = i.incident AND e.date = (SELECT MAX(date) FROM Elements WHERE date <= i.date AND incident = i.incident)
As you can imagine, this takes a while to run on large data sets.Is there a better wya to do this?