I am having difficulty trying to find the best practice for this scenerio.I want to get the top date that is lower than the date of that row. Here is 3 samples that all give me the same results, and I do not really like any of them. Whichi is normally the best practice, I will assume the row number would normally be best, but there are a lot more than 3 records in a live scenerio. Yes, I know to checkt he execution plans, but I am more interested in finding the best practice for this (whether it is listed below, or if there is another method someone is aware of).Thanksdeclare @MyDates Table(d datetime,v char(1))insert into @MyDates(d,v)select '01/01/2005','A' Union allselect '01/01/2007','B' Union allselect '01/30/2007','C'Declare @Myvalues table (d datetime,col1 varchar(5))Insert Into @Myvalues(d,Col1)select '01/01/2006','John' Union allselect '01/01/2009','Tom' Union allselect '01/15/2007','Georg'SELECT d,col1,vFROM (Select row_Number() over ( Partition by a.Col1 order by b.d desc) as rowID,a.*,b.v from@MyValues aInner join@Mydates bon a.d > b.d) AAWHERE AA.rOWid = 1Select a.*,(Select top 1 v from @Mydates where d < a.d order by d desc) as vfrom @MyValues aselect a.* ,b.vfrom @MyValues across apply(Select top 1 * from @MyDates aa where aa.d <a.d order by aa.d desc) b
Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881