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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:03:57+00:00 2026-06-05T11:03:57+00:00

In principle I have 2 tables T1 and T2, each containing a date-field, lets

  • 0

In principle I have 2 tables T1 and T2, each containing a date-field, lets call it date:

T1: date | somekey  | data ... 
T2: date | somekey  | data ...

I want to (left)join T1 and T2, such that the result has all rows from T1.

Now for each row from T1 I need the values from the (exactly one) row of T2 joined where ABS(DATEDIFF(T1.date,T2.date)) is minimal and T1.somekey=T2.somekey (or null in case there is no such row in T2)

  • 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-06-05T11:03:59+00:00Added an answer on June 5, 2026 at 11:03 am

    Don’t do it in the JOIN clause, just put it in the WHERE clause.

    SELECT
    *
    FROM
    T1
    LEFT JOIN T2 ON T1.somekey = T2.somekey
    WHERE
    ABS(DATEDIFF(T1.date,T2.date)) = 1 /*or whatever "minimal" means for you*/
    /*or like this:*/                < $yourMinimalValue
    

    EDIT:

    I think I get what you want to do, but subqueries are the way to go here:

    SELECT
    *
    FROM
    T1
    LEFT JOIN T2 ON T1.somekey = T2.somekey
    WHERE
    ABS(DATEDIFF(T1.date,T2.date)) = (SELECT MIN(DATEDIFF(TT1.date, TT2.date)) FROM  T1 AS TT1 LEFT JOIN T2 AS TT2 ON TT1.somekey = TT2.somekey)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have normalised my database but now I have many extra tables. In principle
I currently have mirroring setup between three computers, principle, mirror, and witness. During the
I have a custom principal object that I want to be able to serialize
I have in the main page with two principal parts : in the left
I have two tables in the DB FuelStation (fuel_station_id: int (PK), fuel_station_name: varchar, fuel_brand_id:
We have some tables for domain type values, ie Customer Type, operation type etc..
My data model contains two tables with composite primary keys and an associative table.
As those who have worked on such a project you probably know this site:
I have a database scheme with versioning data rows, e.g. Table Person has the
I have a large MySQL table (about 750 million rows) and I just want

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.