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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:51:39+00:00 2026-05-26T11:51:39+00:00

I need to join two tables that are described below: Table1: ID Date Info1

  • 0

I need to join two tables that are described below:

Table1:
ID  Date       Info1
1   1/29/2011     i10
1   1/30/2011     i11


Table2:
ID  Date       Info2
1    1/31/2011   i2

I would like to left join the records in Table 2 identified by ID, Month, Year to that in Table 1 identified by the same ID, Month, Year but use the last available record date as the joining record. So for example, in the data above I would join the record in Table 2 to the second record in Table 1 because they match in ID, Month, Year and record 2 of Table 1 has the greatest available day for that (ID, Month, Year) combination. The correct result is:

ID Date Info1 Info2

1 1/30/2011 i11 i2

The SQL code I am coming up with so far is pretty convoluted. Please suggest something. I am using MySQL.

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

    [I want to] …use the last available record date as the joining record

    Solve that first, with a derived table. Assuming that ID, Date is unique, then you can easily group by ID and take the MAX date.

    SELECT
       T1.*,
       T2.*
    FROM Table1 as T1
    JOIN (
       SELECT 
          ID, MAX(Date) as Date
       FROM Table1
       GROUP BY 
          ID
    ) as Last ON
       T1.ID = Last.ID
       AND T1.Date = Last.Date
    LEFT OUTER JOIN Table2 as T2 ON
       T1.ID = Last.ID
       AND MONTH(T1.Date) = MONTH(T2.Date)
       AND YEAR(T1.Date) = YEAR(T2.Date)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I need to join... I want to join table1
I have two tables that I need to join. These are: art id |
I vaguely remember that I need to join the two weird tables to get
I need to join two tables from different MySQL (PHP) connection strings and different
I need to join two tables based on columns with different types. The first
I have two tables that are connected via a join table in a many-to-many
I would like join two tables into 1 table that has one common column
I have three tables that I need to join together and get a combination
For reasons beyond my control, I need to join two tables and I need
I have two tables that I am trying to join in a query, but

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.