Implementing Slowly Changing Dimensions with ODI
In OLTP systems, it is clear that getting back to the previous state of a row that has been updated is very difficult. And if I ask not for the previous state but for what the state of that row was at any given moment, things seem to become even more intractable. Of course, it is possible to build an infrastructure for this in OLTP too, but it is both an extra burden (in every respect) for the OLTP system, laborious, and somewhat unnecessary. This is exactly where the data warehouse, and inevitably the Slowly Changing Dimension method, comes into play.
There are multiple ways to store a table as an SCD, and multiple tools you can use to do so, including PL/SQL and T-SQL. As you might guess, here we will focus on how we can do this in ODI, and try to make it easier to understand with an example.
ODI offers a powerful platform for SCD implementations. Using the features ODI provides, you can easily implement SCDs in your data warehouse projects.
Let’s assume we have a table in which employee information is stored;
CREATE TABLE OLTP.PERSONEL ( PERSONEL_ID NUMBER NOT NULL PRIMARY KEY, AD VARCHAR2(50 BYTE), SOYAD VARCHAR2(50 BYTE), BIRIM_ID NUMBER );
Employee information, especially details such as department and title, is data that can change frequently, and knowing which department an employee was in 4 months ago or 2 years ago is important. To get this information, we need a few extra columns in our target table.
CREATE TABLE KDS.PERSONEL_SCD ( PERSONEL_SK NUMBER, PERSONEL_ID NUMBER, AD VARCHAR2(50 BYTE), SOYAD VARCHAR2(50 BYTE), BIRIM_ID NUMBER, BAS_TAR DATE, BIT_TAR DATE );
Let’s look at why we added the BAS_TAR and BIT_TAR columns to the target table;
For example, employee A was in the Software department from 2005 to 2009, and has been in the R&D department since 2009.
On the OLTP side, until 01.01.2009;

you would see it like this. Therefore, if you query the table on the DW side with a date parameter, it is now very easy to find out where the employee was on which date. And the columns that give us this convenience are BAS_TAR and BIT_TAR. (the column names are optional :))
As you will probably have noticed from the example above, we can no longer speak of the PERSONEL_ID column being unique, so we need a new unique column (Surrogate Key), and that is PERSONEL_SK.
Assuming that both the source and target tables have been created and imported into the ODI model, we can start preparing the SCD interface.
First, we need to open the target table PERSONEL_SCD from the model, go to the column editor, and set the Slowly Changing Dimension Behavior values in the Description section.

In our example; PERSONEL_SK = Surrogate Key, PERSONEL_ID = Natural Key, AD, SOYAD, BIRIM_ID = Add Row On Change, BAS_TAR = Starting Timestamp, BIT_TAR = Ending Timestamp should be set.
After opening a new interface screen and completing the definition section, we can switch to the Mapping tab and drag our tables to the relevant places. If we have not changed the column names, ODI will automatically map all columns except the newly added ones.

The Surrogate Key field should take its value from a sequence. After defining the sequence we will use in ODI, you can enter SEQUENCE_ADI_NEXTVAL in the mapping of the Surrogate Key field. Again in the mapping section, let’s use SYSDATE for BAS_TAR and a date far in the future for BIT_TAR. The final state of our mapping should look similar to the one below.

After completing the mapping stage, we can switch to the Flow tab and select the SCD Knowledge Module we will use. Assuming our database is Oracle, I selected IKM Oracle Slowly Changing Dimension. (If it is not there, you can import it)

After selecting the module and (for simple usage) setting the Flow Control option to false, we can save the interface.
When you run the interface for the first time, it loads the current values of all records into the SCD table with the current date. To test it, in our own example let’s insert two records into the source table, run the interface, and then check the target table.
INSERT INTO OLTP.PERSONEL (PERSONEL_ID, AD, SOYAD, BIRIM_ID) VALUES (157, ‘SUNA’, ‘SEYHAN’, 1);
INSERT INTO OLTP.PERSONEL (PERSONEL_ID, AD, SOYAD, BIRIM_ID) VALUES (441, ‘BÜLENT’, ‘ŞEN’, 1);
The target table (KDS.PERSONEL_SCD) after the interface has run for the first time;

let’s see what happens when we update one of the records in the source table and run the interface again.
UPDATE OLTP.PERSONEL SET BIRIM_ID = 45 WHERE PERSONEL_ID = 147;
The target table resulting from the update and the subsequent re-run of the interface;

As you can see, the updated row has been duplicated, and both the old and new versions of the record are kept. For example, we can find out which department the employee with ID 157 was in on which date by using a date parameter. Good luck.
