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

  • Home
  • SEARCH
  • 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 8736397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:17:43+00:00 2026-06-13T10:17:43+00:00

I have two history tables that track changes in database values, using a revision

  • 0

I have two history tables that track changes in database values, using a revision id to track the individual changes. e.g.

Table 1:

 rev |  A   |  B 
=================
 1   |  100 | 'A'
 4   |  150 | 'A'
 7   |  100 | 'Z'

Table 2:

 rev |  C   |  D 
==================
 1   |  200 | True
 5   |    0 | True
 8   |    0 | False

The goal would be to merge the two tables into:

 rev |  A   |  B  |  C  |  D 
===============================
 1   |  100 | 'A' | 200 | True
 4   |  150 | 'A' | 200 | True
 5   |  150 | 'A' |   0 | True
 7   |  100 | 'Z' |   0 | True
 8   |  100 | 'Z' |   0 | False

The idea being that for for a given revision, I would take the values corresponding to that revision or the highest revision less than it.

The SQL query that comes to mind would be something akin to cross joining the two tables with constraint rev1 < rev2, then selecting rows using a subquery where rev1 = max(rev1) for each given rev2; unioning this query with its counterpart exchanging rev2 and rev1; and finally filtering out duplicates from where rev1 = rev2.

The questions are:

  • Is there a name for this type of join?
  • Is there an idiom for performing this type of join in SQL, or would it be better to do it programmatically (which would definitely be much simpler and possibly more efficient)?
  • 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-13T10:17:44+00:00Added an answer on June 13, 2026 at 10:17 am

    SQL Fiddle

    select
        coalesce(t1.rev, t2.rev) rev,
        coalesce(a, lag(a, 1) over(order by coalesce(t2.rev, t1.rev))) a,
        coalesce(b, lag(b, 1) over(order by coalesce(t2.rev, t1.rev))) b,
        coalesce(c, lag(c, 1) over(order by coalesce(t1.rev, t2.rev))) c,
        coalesce(d, lag(d, 1) over(order by coalesce(t1.rev, t2.rev))) d
    from
        t1
        full join
        t2 on t1.rev = t2.rev
    order by rev
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a database table that keeps data with revision history (like
I have two MySQL tables (product and price history) that I would like to
I have two tables. First one: create table history ( id integer primary key,
I have two tables, songs and history. The songs table looks like: ID |
Say I have two tables, a live table and a history table. The live
I've got a scenario where I have a database with one table that gets
Say that you have a table with two (or more) columns which link to
I have two tables almost identical: employee employee_history The second table has two additional
Okay, so I have these two tables in MySQL. One is called 'History' and
Okay, so I have these two tables in MySQL. One is called 'History' and

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.