Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6185133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:39:34+00:00 2026-05-24T01:39:34+00:00

Using SQL2008, I’m trying to figure out an efficient query to find a row

  • 0

Using SQL2008, I’m trying to figure out an efficient query to find a row whose date is nearest to a specific target date.

There are obvious inefficient solutions (e.g. table scan using ABS and DATEDIFF) which I didn’t bother looking at, because my table already has a covering index where the date is the first column. I can narrow down the results using that index before figuring out exactly which row is nearest.

In theory I should be able to satisfy the query using a single index lookup and then a sequential pull of 2 rows of data from that index.

But so far I’ve been unable to find a more optimal solution than this one:

DECLARE @target DATETIME = '01/02/2011'

SELECT TOP 1 Val, Measured
FROM (
   SELECT TOP 1 Val, Measured 
       FROM tbl 
       WHERE Measured <= @Target 
       ORDER BY Measured desc
   UNION ALL
   SELECT TOP 1 Val, Measured 
       FROM tbl 
       WHERE Measured >= @Target 
       ORDER BY Measured asc
) x
ORDER BY ABS (DATEDIFF (second, Measured, @Target))

This is fast (4 logical reads on the test schema below, 9 logical reads in my real-world table) but it’s still a 2-scan-count solution. Is there a more efficient solution which only hits this index once?

Or is my existing solution “good enough” because that second index seek will be pulling cached pages accessed by the first seek, meaning it will be so fast that further optimzation (even if possible) will yield minimal actual perf improvement?

Here’s the schema and some sample data. Both are simplified from my actual schema, although the resulting query plan is the same as my more complex table:

CREATE TABLE tbl
(
    ID int IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
    Measured DATETIME NOT NULL,
    Val int NOT NULL
);
CREATE NONCLUSTERED INDEX IX_tbl ON tbl (Measured) INCLUDE (Val)
INSERT tbl VALUES ('2011-01-01 12:34',6);
INSERT tbl VALUES ('2011-01-01 23:34',6);
INSERT tbl VALUES ('2011-01-03 09:03',12);
INSERT tbl VALUES ('2011-02-01 09:24',18);
INSERT tbl VALUES ('2011-02-08 07:12',7);
INSERT tbl VALUES ('2011-03-01 12:34',6);
INSERT tbl VALUES ('2011-04-03 09:03',12);
INSERT tbl VALUES ('2011-05-01 09:24',18);
INSERT tbl VALUES ('2011-06-08 07:12',7);
-- insert another few million rows here to compare to my real-world table
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:39:34+00:00Added an answer on May 24, 2026 at 1:39 am

    Consider first determining where in the table your @target is, then limiting the scope of your search for +1 / -1 to within a day or a week first. Then sorting by date within that set to find the closest will cost less than applying TOP 1/ORDER BY to the whole set on each side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
I'm using sql 2008 express edition and I'm trying to do a multiple row
I am trying to figure out how to compare the current day's data to
HI all, Iam using VS2008 and SQL2008 i want to access my stored procedures
How can I perform an idempotent insert row using subsonic with a SQL 2008
This is something I just couldn't figure out how to do in ASP.NET: I
I'm trying to troubleshoot this problem using SQL Profiler (SQL 2008). After days of
I've a really strange problem getting IIS7 to connect to Sql2008. First, using the
I have a VB6 project that is using a SQL2008 database. The project consists
I want to create a table using the results of a query by utilizing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.