Loop in Db2 Stage/Any alternate approach with DS stages

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
synsog
Premium Member
Premium Member
Posts: 232
Joined: Sun Aug 01, 2010 11:01 pm
Location: Pune

Loop in Db2 Stage/Any alternate approach with DS stages

Post by synsog »

Hi All,

Can someone pls shed some light around how to accomplish the below logic in Datastage.

I have an input file with the below values

Input :
---------
P1
P2
P3


I have another DB2 Table where i will have record with P1 value . The input record with value P1 is combined to P2 & P2 is combined to P3 & P3 is combined to P4 in the Db2 table (as shown in the below example)

Input fields
P : P1
-------------
P1 P2
P2 P3
P3 P4


The resultant output I am expecting is as below

O/P
-------------
P1 P4


Any ideas on how to accomplish this is appreciated.

Thanks
Synsog
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

So, to put it another way, you want to traverse a hierarchy and return the start and end points?
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
synsog
Premium Member
Premium Member
Posts: 232
Joined: Sun Aug 01, 2010 11:01 pm
Location: Pune

Post by synsog »

Hi Ray - Yes that s correct.

In the Table, record with P1 is combined to P2 (making P2 the latest value corresponding to P1 value). A few days later, P2 might get combined to P3 (making P3 the latest) & P3 to P4.

I am trying to find what is the latest 'P' value P1 is currently holding.

ie
P1=P2
P2=P3
P3=P4

so, what is the latest value of P1 ? (Output should be P4 in this case)

Thanks
Synsog
BI-RMA
Premium Member
Premium Member
Posts: 463
Joined: Sun Nov 01, 2009 3:55 pm
Location: Hamburg

Post by BI-RMA »

You can do this by using multiple left self-joins on the db2-table:

select table1.P,
case when tablen.P1 is not null then tablen.P1
...
when table3.P1 is not null then table3.P1
when table2.P1 is not null then table2.P1
else table1.P1 end P1
from table table1
left join table table2
on table1.P = table2.P1
left join table table3
on table2.P = table3.P1
...
left join table tablen
on table3.P = tablen.P1;

Can be done using DataStage-left-joins as well. Either way you need to know the maximum number of hierarchies within your data to get the correct result.
"It is not the lucky ones are grateful.
There are the grateful those are happy." Francis Bacon
synsog
Premium Member
Premium Member
Posts: 232
Joined: Sun Aug 01, 2010 11:01 pm
Location: Pune

Post by synsog »

Thanks Roland,

I am not sure whether i understood the below logic. Can you pls tell me what Table n...3,2,1 etc is ?

Also, can you pls shed some light on how we can do it using Datastage self joins ?

Thanks
Synsog
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

I would suggest using a Loop in a Transformer stage, using the SaveInputRecord() and GetSavedInputRecord() functions to manipulate the stack of records, as well as LastRecordInGroup() with a dummy constant argument column to exit from the loop.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply