Solved

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

Posted on 2009-05-19
3
298 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
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.

856 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