SQL2005 - Need to find nearest previous time in a table row to a given time
Posted on 2009-05-19
Hi, i have some data in a table representing historical prices. I need to find the data from a column "price" of the closest time prior to the given time where the product ID is equal to my product ID. I can get to this but it may not be the most efficient SQL query. Can someone recommend the most efficient SQL for this?
price_datetime varchar(16) (format is "2009051915070100")
I have the product_id in a variable @product_id and time_stamp in @price_time. There are millions of rows in the data table, and prices may be within milliseconds of each other. If my @price_time value was "2009051912570875" I need to find the single row in the data table where the price_datetime value immediately precedes the price time given.