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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:14:09+00:00 2026-05-29T11:14:09+00:00

I have 3 tables that I want to merge, each with a different column

  • 0

I have 3 tables that I want to merge, each with a different column of interest. I also have an id variable that I want to do separate merges “within” id. The idea is that I want to merge X, Y, and Z by date (within ID), and have missing values if that date does not exist for a particular variable.

Table X:
ID     Date         X
1      2012-01-01   101
1      2012-01-02   102
1      2012-01-03   103
1      2012-01-04   104
1      2012-01-05   105
2      2012-01-01   150

Table Y:
ID     Date         Y
1      2012-01-01   301
1      2012-01-02   302
1      2012-01-03   303
1      2012-01-11   311
2      2012-01-01   350

Table Z:
ID     Date         Z
1      2012-01-01   401
1      2012-01-03   403
1      2012-01-04   404
1      2012-01-11   411
1      2012-01-21   421
2      2012-01-01   450

Desired Result Table:
ID     Date         X     Y     Z
1      2012-01-01   101   301   401
1      2012-01-02   102   302   .
1      2012-01-03   103   303   403
1      2012-01-04   104   .     404
1      2012-01-05   105   .     .
1      2012-01-11   .     311   411
1      2012-01-21   .     .     421
2      2012-01-01   150   350   450

Any ideas how to write this SQL statement? I’ve tried messing around with “full joins” and where statements for cross products, but I keep getting duplicate values for some of my ID-date combinations, or sometimes no ID.

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-29T11:14:10+00:00Added an answer on May 29, 2026 at 11:14 am

    Joins can be tricky things. My usual approach is to form the set of Keys first, and then use those keys to get what I want.

    SELECT source.ID, source.Date, x.X, y.Y, z.Z
    FROM
    (
      SELECT ID, Date
      FROM TableX
      UNION
      SELECT ID, Date
      FROM TableY
      UNION
      SELECT ID, Date
      FROM TableZ
    ) as source
    LEFT JOIN TableX x ON source.ID = x.ID AND source.Date = x.Date
    LEFT JOIN TableY y ON source.ID = y.ID AND source.Date = y.Date
    LEFT JOIN TableZ z ON source.ID = z.ID AND source.Date = z.Date
    ORDER BY source.ID, source.Date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 different tables that I want to join. The tables are structured
I have more tables and I want to merge that some table for getting
I have two tables that I want to query and merge based on a
I have a two mysql tables that I want to merge together in one
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have several tables that I want to put side by side, however when
If i have two tables that have nothing in common I want to do
I have 2 tables with some columns and another table that I want its
I have a series of tables that contain data I want to full text
I have two tables and I want to display only the child ID that

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.