Solved

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

Posted on 2009-05-19
3
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

710 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