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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:40:47+00:00 2026-05-25T11:40:47+00:00

I am trying to write a Microsoft SQL Server query for retrieving the oldest

  • 0

I am trying to write a Microsoft SQL Server query for retrieving the oldest record in which the text fields are the same, but the dates are 30 seconds or less apart. Here is an example:

My table:

RecordID     TextField1              TextField2           DateField1
--------------------------------------------------------------------------------
1            SomeData1               SomeData2            9/11/2011 2:33:00pm
2            SomeData3               SomeData4            9/11/2011 2:33:15pm
3            SomeData3               SomeData4            9/11/2011 2:33:18pm
4            SomeData3               SomeData4            9/11/2011 2:42:12pm
5            SomeData1               SomeData2            9/11/2011 2:33:01pm
6            SomeData6               SomeData7            9/11/2011 2:33:01pm
7            SomeData1               SomeData2            9/12/2011 2:33:00pm
8            SomeData6               SomeData8            9/11/2011 2:33:03pm

Okay, so in this example, I want a query that will pull the rows in which TextField1=TextField1 and TextField2=TextField2 and the dates between them are 30 seconds or less (I want the oldest of the two returned). So the query, in this example, should return:

RecordID     TextField1              TextField2           DateField1
--------------------------------------------------------------------------------
1            SomeData1               SomeData2            9/11/2011 2:33:00pm
2            SomeData3               SomeData4            9/11/2011 2:33:15pm

RecordID 8 is not returned because TextField2 is different.

Hopefully I explained this clearly enough. Any help would be appreciated!

  • 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-25T11:40:48+00:00Added an answer on May 25, 2026 at 11:40 am

    I couldn’t understand everything on your question.

    This is a generic SQL query that will compare the records of you table, against the same table, looking for records with different RecordID, but equal TextField1 and TextField2.

    Leave a comment if this looks like what you want and we can improve this query to get exactly what you are looking for.

    UPDATED:

    SELECT * FROM my_table AS t1
    INNER JOIN my_table AS t2
    ON (
        t1.RecordID < t2.recordID
        AND
        DATEDIFF(second, t1.DateField1, t2.DateField1) <= 30
        AND
        t1.TextField1 = t2.TextField1
        AND
        t2.TextField2 = t1.TextField2
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a query to find records which don't have a matching record
I'm trying to write this as part of stored procedure on SQL Server 2000.
I'm relatively inexperienced at SQL Server, but am trying to troubleshoot some of the
I'm trying to migrate a MySQL-based app over to Microsoft SQL Server 2005 (not
I'm trying to write an LDAP query which will discover if a user is
I'm trying to write a dll plugin for Winamp. I'm using Microsoft Visual Studio
I'm trying to write a blog post which includes a code segment inside a
I am trying to write the following query on postgresql: select name, author_id, count(1),
I'm trying to install SQL Server 2005 Developer Edition on my Windows 7 Ultimate
I'm trying to write a script which automatically restores a database backup. I know

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.