In this approach usually only the current and previous value of dimension is kept in the database. The new value is loaded into 'current/new' column and the old one into 'old/previous' column. Generally speaking the history is limited to the number of column created for storing historical data.
For example we can have the city name or state name.
See the below.
ID |
Name |
Current_City |
Previous_City |
1 |
Bagesh Kumar Singh |
Chennai |
Madras |
2 |
Mahesh |
Mumbai |
Bombay |
If we want to implement this approach we need to alter the dimension
table and need to make the changes in the load script or load ETL package.
Let’s see the demo
We have DimCustomer_SCD_3 table with below columns.
CREATE TABLE dimcustomer_scd_3_stg (
dimcustomer_scd_3_stg_id INT IDENTITY(1, 1) PRIMARY KEY, cus_id INT, cus_nm VARCHAR(50), Current_City varchar(20) ) CREATE TABLE dimcustomer_scd_3 ( dimcustomer_scd_3_id INT IDENTITY(1, 1) PRIMARY KEY, cus_id INT, cus_nm VARCHAR(50), Current_City varchar(20) ) |
See the table
Now business want to store the previous city name in that
case we need to alter the both table and also we need to make the changes in to
the load script or load ETL package.
Altering the stg and main table.
ALTER TABLE dimcustomer_scd_3 |
Now we are updating the logic to load the previous city
name. we are inserting the below records into the stage table for load.
INSERT INTO dimcustomer_scd_3_stg |
See the records in the both table.
Below is the script use to populate the data. If we get the
previous city value then we are updating it if not the keeping as null.
MERGE dimcustomer_scd_3 TT |
Running this script.
Record updated successfully.
No comments:
Post a Comment
If you have any doubt, please let me know.