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 1094865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:02:04+00:00 2026-05-17T00:02:04+00:00

I have two tables and I’m looking for the rows in one table where

  • 0

I have two tables and I’m looking for the rows in one table where a time column is not near any of the values in another table’s time column. (Near is defined as within a minute).

Here’s a code sample:

create table temp1
(
    id int identity primary key,
    value datetime not null 
)
GO

create index ix_temp1 on temp1(value, id);
GO

set nocount on
insert temp1 (value) values (DATEADD(second, rand() * 1000000, '20100101'))
GO 15000

table temp2 is set up identical:

create table temp2
(
    id int identity primary key,
    value datetime not null 
)
GO

create index ix_temp2 on temp2(value, id);
GO

set nocount on
insert temp2 (value) values (DATEADD(second, rand() * 1000000, '20100101'))
GO 15000

And here’s my first crack at it (which is very inefficient)

SELECT t1.id, t1.value
FROM temp1 t1
LEFT JOIN temp2 t2
    ON t1.value between DATEADD(MINUTE, -1, t2.value) and DATEADD(MINUTE, 1, t2.value)
WHERE t2.value is null

I’m looking for ways to do this more efficiently. All solutions will be considered (new indexes, SSIS solution, CLR solutions, temp tables, cursors etc…)

  • 1 1 Answer
  • 2 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-17T00:02:05+00:00Added an answer on May 17, 2026 at 12:02 am

    The LEFT JOIN/IS NULL isn’t as efficient on SQL Server as NOT IN or NOT EXISTS when columns are not nullable – see this link for details.

    That said, this:

    SELECT t1.id,
           t1.value
      FROM temp1 t1
     WHERE NOT EXISTS(SELECT NULL
                        FROM temp2 t2
                       WHERE t2.value BETWEEN DATEADD(MINUTE, -1, t1.value)  
                                          AND DATEADD(MINUTE, 1, t1.value))
    

    …still has a problem in that function use (IE: DATEADD) renders the index useless. You’re altering the data of the column (temporarily, without writing it back to the table) while the index is on the original value.

    I’m at a loss for options if you want the precision. Otherwise, if you alter the datetime before it’s inserted into the temp table then you gain:

    1. ability to straight compare: t1.value = t2.value
    2. ability to use the index, assuming optimizer believes it can be of use
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables connected with one to many relationship. Parent Table is a
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables: one table (okay, it's a view), vComputer, lists many computers
I have two tables that I want to join into one table and use
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username

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.