Solved

SQL2005 - Need to find nearest previous time in a table row to a given time

Posted on 2009-05-19
3
297 Views
Last Modified: 2013-12-07
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?


Data table
product_id         varchar(100)
price_datetime  varchar(16)  (format is "2009051915070100")
price                   float

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.

Thanks
Mike
0
Comment
Question by:mikegrad7
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 24425614
For this to be a fast query against a table with millions of rows, I assume you have indexes on product and price_datetime so this should work:
select *
from DataTable
where product_id = @product_id
and price_datetime = (
    select max(price_datetime)
    from DataTable
    where price_datetime < @price_time
    and product_id = @product_id)
0
 
LVL 1

Author Comment

by:mikegrad7
ID: 24425680
Thanks, that is what i was doing - is this the only or most efficient way? And yes, there are indexes on both product and time.
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24425904
If the table is indexed, this is the fastest way I have found to get this data.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question