Teradata – Merging two change history tables

Have you encountered a poorly designed physical data model where object columns are distributed randomly across tables, and you wish to unify them in their rightful place?

Merging non-historical tables is simple, but the process becomes more complex when historization is applied to at least one of the tables.

Change history tables are typically complex. However, you need not worry; we have a solution to simplify your task. We will guide you through all the steps using a small example.

CREATE MULTISET VOLATILE TABLE <JOIN_TYPE_LOOKUP> (
JOIN_TYPE CHAR(4)
) ON COMMIT PRESERVE ROWS;
INSERT INTO <JOIN_TYPE_LOOKUP> VALUES ('before');
INSERT INTO <JOIN_TYPE_LOOKUP> VALUES ('between');
INSERT INTO <JOIN_TYPE_LOOKUP> VALUES ('after');

— Two historized test tables

CREATE MULTISET VOLATILE TABLE <TABLE_A> (
ID_A INTEGER,
RSD DATE,
RED DATE
) ON COMMIT PRESERVE ROWS;
CREATE MULTISET VOLATILE TABLE <TABLE_B> (
ID_B INTEGER,
RSD DATE,
RED DATE
) ON COMMIT PRESERVE ROWS;

INSERT INTO <TABLE_A> VALUES (1, ‘2014-01-01’, ‘2014-01-31’);
INSERT INTO <TABLE_B> VALUES (1, ‘2014-01-31’, ‘9999-12-31’);

SELECT
--------- Block for columns from table A ---------
CASE
WHEN JOIN_TYPE = 'before' AND a.RSD < b.RSD THEN ID_A
WHEN JOIN_TYPE = 'before' AND b.RSD < a.RSD THEN NULL
WHEN JOIN_TYPE = 'between' THEN ID_A
WHEN JOIN_TYPE = 'after' AND a.RED > b.RED THEN ID_A
WHEN JOIN_TYPE = 'after' AND b.RED > a.RED THEN NULL
END AS ID_A,
--------- Block for columns from table B ---------
CASE
WHEN JOIN_TYPE = 'before' AND a.RSD < b.RSD THEN NULL
WHEN JOIN_TYPE = 'before' AND b.RSD < a.RSD THEN ID_B
WHEN JOIN_TYPE = 'between' THEN ID_B
WHEN JOIN_TYPE = 'after' AND a.RED > b.RED THEN NULL
WHEN JOIN_TYPE = 'after' AND b.RED > a.RED THEN ID_B
END AS ID_B,
-------- Identify new RSD/RED values --------
CASE
WHEN JOIN_TYPE = 'before' AND a.RSD < b.RSD THEN a.RSD
WHEN JOIN_TYPE = 'before' AND b.RSD < a.RSD THEN b.RSD
WHEN JOIN_TYPE = 'between' AND a.RSD < b.RSD THEN b.RSD
WHEN JOIN_TYPE = 'between' AND b.RSD <= a.RSD THEN a.RSD
WHEN JOIN_TYPE = 'after' AND a.RED < b.RED THEN a.RED + 1
WHEN JOIN_TYPE = 'after' AND b.RED < a.RED THEN b.RED + 1
WHEN JOIN_TYPE = 'between' THEN (coalesce(a.RSD, b.RSD))
END AS new_RSD,
CASE
WHEN JOIN_TYPE = 'before' AND a.RSD < b.RSD THEN b.RSD - 1
WHEN JOIN_TYPE = 'before' AND b.RSD < a.RSD THEN a.RSD - 1
WHEN JOIN_TYPE = 'between' AND a.RED < b.RED THEN a.RED
WHEN JOIN_TYPE = 'between' AND b.RED <= a.RED THEN b.RED
WHEN JOIN_TYPE = 'after' AND a.RED < b.RED THEN b.RED
WHEN JOIN_TYPE = 'after' AND b.RED < a.RED THEN a.RED
WHEN JOIN_TYPE = 'between' THEN (coalesce(a.RED, b.RED))
END AS new_RED
---------------------------------------------------
from <TABLE_A> a
FULL OUTER JOIN
<TABLE_B> b
ON (a.RSD, a.RED+1) OVERLAPS (b.RSD, b.RED+1)
-- further join conditions
CROSS JOIN <JOIN_TYPE_LOOKUP> h
WHERE
new_RSD IS NOT NULL
AND new_RED IS NOT NULL;

Please note the language used in this text:

I assume that you are familiar with the concept of volatile tables and the basic concepts in historisation. RSD and RED stand for the record start and end dates.

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.