Chapter-3
Introduction
In Informatica we can store the value of a field(s) of previous record and compare it with the current record using an Expression transformation.
This is quite helpful in order to identify the change in the data and take the actions accordingly. Let us understand how to compare current record with previous record with examples.
Consider the following as the Source data
ITEM
-----
a
b
c
a
c
d
c
e
d
Q1. Design a mapping to load the all the items with no duplicate records into one target and the items with duplicates into another target.
Expected Output:
ITEM |
---|
b |
e |
ITEM |
---|
a |
a |
c |
c |
c |
d |
d |
Solution
- Select the object with above data as source and pass the data to a sorter transformation.
- Sort the data based on ITEM with the Sort Order as Ascending.
- Pass the data from Sorter to an Aggregator transformation.
- In aggregator, select ITEM as the Group By field. In the Aggregate section, create a new field Count and assign value as COUNT(ITEM)
Count=COUNT(ITEM) - At this stage the output from the aggregator will be as below
ITEM, COUNT
a,2
b,1
c,3
d,2
e,1 - Now take a Joiner transformation into the mapping canvas and pass the data from Sorter and Aggregator to it.
- There will be Field Name conflicts as the field with name ITEM is passed from both sorter and aggregator. In order to resolve it, add a prefix to the sorter data as “SRT_”.
- Select the Join Type as Normal and Join Condition as SRT_ITEM = ITEM
- In the joiner transformation check the Sorted Input property, else the mapping will fail.
- Pass the data from joiner to a Router transformation. Create two groups with conditions as below
Group Name Condition
Unique Count=’1’
Duplicate Count>’1’ - Map the data from Unique group one target and Duplicate group to another target.
The final mapping will be as below.
If you are wondering where we have compared current record with previous record, we haven’t. This actually helps us understand our next example where we compare the current record with previous record.
Q2. Design a mapping to load the all the unique items into one target and the duplicates into other.
Expected Output:
ITEM |
---|
a |
b |
c |
d |
e |
ITEM |
---|
a |
c |
c |
d |
Solution
- Select the object with above mentioned data as source and pass the data to a sorter transformation.
- Sort the data based on ITEM with the Sort Order as Ascending.
- Pass the data from Sorter to an Expression transformation.
- In the expression, create a variable field V_PrevItem and assign value as ITEM. This field is used to hold the item value of the previous record.
- Create an another variable field V_count to assign the incremental values to each record. This value needs to be reset whenever the item changes.
V_count = IIF(ITEM!=V_PrevItem,1,V_count+1) - Finally create an output field O_count and set the value as V_count.
- The expression fields will be as below.
V_count = IIF(ITEM!=V_PrevItem,1,V_count+1)
V_PrevItem = ITEM
O_count = V_count
NOTE: The order of fields is important here because if we assign the value to the V_PrevItem before calculating V_count, then both ITEM and V_PrevItem will have same value which is not expected. - The output from the expression transformation will be as below
ITEM, O_count
a,1
a,2
b,1
c,1
c,2
c,3
d,1
d,2
e,1 - Pass the data from joiner to a Router transformation. Create two groups with conditions as below
Group Name Condition
Unique O_count=’1’
Duplicate O_count>’1’ - Map the data from Unique group to one target and Duplicate group to another target.
The final mapping will be as below.
Understand the difference between Q1 and Q2. In order to find the duplicates, we can use an aggregator and get the count of each Item. Then using a joiner, we assigned the count value to each record. But in order to assign an incremented value to each item individually we have to track the change in the item value. This can be achieved by comparing the current and previous record values.
Let us look at some more examples to understand the concept better.
Consider the following as the Source data
EMP_ID | DEPT_ID | SALARY |
---|---|---|
101 | 1111 | 2000 |
102 | 1111 | 3000 |
103 | 1111 | 4000 |
104 | 2222 | 5000 |
105 | 2222 | 6000 |
106 | 2222 | 7000 |
Q3. Design a mapping to calculate cumulative sum of salary of employees.
Expected Output:
EMP_ID | DEPT_ID | SALARY | Cum_Salary |
---|---|---|---|
101 | 1111 | 2000 | 2000 |
102 | 1111 | 3000 | 5000 |
103 | 1111 | 4000 | 9000 |
104 | 2222 | 5000 | 14000 |
105 | 2222 | 6000 | 20000 |
106 | 2222 | 7000 | 27000 |
Solution
- Select the object with above mentioned data as source and pass the data to an expression transformation.
- In the expression create a variable field V_CumSalary and assign value as V_CumSalary+Salary.
- Create an output field Cum_Salary and assign value as V_CumSalary.
- The fields in the expression will be as below.
V_CumSalary = V_CumSalary+Salary
Cum_Salary = V_CumSalary - Pass the data from expression to a target transformation and configure the target.
We have created a variable and started adding salary values to it for each record here.
Q4. Design a mapping to calculate cumulative sum of salary of employees Department wise.
Expected Output:
EMP_ID | DEPT_ID | SALARY | Cum_Salary |
---|---|---|---|
101 | 1111 | 2000 | 2000 |
102 | 1111 | 3000 | 5000 |
103 | 1111 | 4000 | 9000 |
104 | 2222 | 5000 | 5000 |
105 | 2222 | 6000 | 11000 |
106 | 2222 | 7000 | 18000 |
For the record 104 the cumulative salary value is set as salary value as the department got changed from 1111 to 2222.
Solution:
- Select the object with above mentioned data as source and pass the data to a sorter transformation.
- In sorter, sort the data on DEPT_ID and EMP_ID with sort order as ascending.
- Pass the data from sorter to an expression transformation.
- In the expression, create a variable field V_PrevDeptID and assign value as DEPT_ID. This field is used to hold the department id value of the previous record.
- Create an another variable V_CumSalary and assign value as IIF(DEPT_ID!=V_PrevDeptID, SALARY,V_CumSalary+SALARY)
- Finally create an output field Cum_Salary and assign value as V_CumSalary.
- The expression fields will be as below.
V_CumSalary = IIF(DEPT_ID!=V_PrevDeptID, SALARY,V_CumSalary+SALARY)
Cum_Salary = V_CumSalary
V_PrevDeptID = DEPT_ID - Pass the data from expression to a target transformation and configure the target.
Notice that whenever the department ID changes, we are setting the cumulative salary value as the salary value of the record else we are calculating the sum.
In Q3, the final record will have the total sum of the salary of employees. In Q4, the final record of each department will have the total sum of salary of the employees of that department.
Consider the following as the Source data
DEPT_ID | ITEM |
---|---|
111 | A |
111 | B |
111 | C |
111 | D |
222 | E |
222 | F |
222 | G |
222 | H |
Q5. Design a mapping to concatenate the item values in a table
Expected Output:
DEPT_ID | ITEM | ITEM_LIST |
---|---|---|
111 | A | A |
111 | B | A,B |
111 | C | A,B,C |
111 | D | A,B,C,D |
222 | E | A,B,C,D,E |
222 | F | A,B,C,D,E,F |
222 | G | A,B,C,D,E,F,G |
222 | H | A,B,C,D,E,F,G,H |
Solution:
- Select the object with above mentioned data as source and pass the data to a sorter transformation.
- In sorter, sort the data on DEPT_ID and ITEM with sort order as ascending.
- In the expression create a variable field V_Count and assign value as V_Count+1. This is to identify the first item.
- Create an another variable V_ItemList and assign value as IIF(V_Count=1, ITEM, V_ ItemList ||’,’||ITEM)
- Finally create an output field Item_List and assign value as V_ItemList.
- The expression fields will be as below
V_Count = V_Count+1
V_ItemList = IIF(V_Count=1, ITEM, V_ ItemList ||’,’||ITEM)
Item_List = V_ItemList - Pass the data from expression to a target transformation and configure the target.
So using the count variable we are identify the first record and anything after that is getting concatenated to the previous calculated value.
Q6. Design a mapping to concatenate the item values in a table department wise.
Expected Output:
DEPT_ID | EMP_NAME | EmpList |
---|---|---|
111 | A | A |
111 | B | A,B |
111 | C | A,B,C |
111 | D | A,B,C,D |
222 | E | E |
222 | F | E,F |
222 | G | E,F,G |
222 | H | E,F,G,H |
Solution:
- Select the object with above mentioned data as source and pass the data to a sorter transformation.
- In sorter, sort the data on DEPT_ID and ITEM with sort order as ascending.
- In the expression, create a variable field V_PrevDeptID and assign value as DEPT_ID. This field is used to hold the department id value of the previous record.
- Create an another variable V_ItemList and assign value as IIF(DEPT_ID!=V_PrevDeptID, ITEM, V_ ItemList ||’,’||ITEM)
- Finally create an output field Item_List and assign value as V_ItemList.
- The expression fields will be as below.
V_ItemList = IIF(DEPT_ID!=V_PrevDeptID, ITEM, V_ ItemList ||’,’||ITEM)
Item_List = V_ItemList
V_PrevDeptID = DEPT_ID - Pass the data from expression to a target transformation and configure the target.
Notice that whenever the department ID changes, we are setting the list value as the Item value of the record else we are concatenating the item values.
In Q5, the final record will have the total concatenated list of the item values. In Q6, the final record of each department will have the total concatenated list of items of that department.
Conclusion
In the examples discussed above we have compared records in Q2, Q4 and Q6 only.
But the other examples Q1, Q3 and Q5 scenarios can be easily converted into Q2, Q4 and Q6 and they help us understand the scenario and need to compare the current and previous record.
Compare current record with previous record whenever it is necessary to track the change in the data by storing the previous record value in a variable field in expression.
great learned a valuable technique involving variable to keep before values