Current Row and Previous Row

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
Ahimsa
Premium Member
Premium Member
Posts: 78
Joined: Fri Aug 18, 2006 11:05 pm

Current Row and Previous Row

Post by Ahimsa »

I want to develop a logic in which I check the column value of current row and earlier row. If its different, I want to increment the counter by 1.

e.g. Row 1.Name = A
Row2.Name = A
Row3.Name = B
Row4.Name = C

So here Name is changed from A to B to C, there I will be incrementing the counter by 1. If its same Name no counter increment.

Any suggestion on how to remember the column value of earlier row and compare it to current row.

Thanks.
nick.bond
Charter Member
Charter Member
Posts: 230
Joined: Thu Jan 15, 2004 12:00 pm
Location: London

Post by nick.bond »

Stage variables are processed in order, you can therefore store values from the previous record in them
svPreviousValue = svCurrentValue
svCurrentValue = input.field
svValueHasChanged = If svCurrentValue = svPreviousValue Then 0 Else 1
svCounter = If svValueHasChanged Then svCounter + 1 Else svCounter
Regards,

Nick.
JoshGeorge
Participant
Posts: 612
Joined: Thu May 03, 2007 4:59 am
Location: Melbourne

Post by JoshGeorge »

In case you want to do this for record groups:

Use a sort stage. Sort records based on your key. Add SORT stage property Create Key Change Column. This property will add a column to your output link. The value of this column is 1 for the first record in the sort record group else the value is 0. In transformer use stage variables as stated in the above post. When a new record group comes the first record will be coming with key change column as 1, that time reset the svCounter to 1.
Joshy George
<a href="http://www.linkedin.com/in/joshygeorge1" ><img src="http://www.linkedin.com/img/webpromo/bt ... _80x15.gif" width="80" height="15" border="0"></a>
vmcburney
Participant
Posts: 3593
Joined: Thu Jan 23, 2003 5:25 pm
Location: Australia, Melbourne
Contact:

Post by vmcburney »

Since this is a parallel job make sure you have sorted and partitioned by your Name field so that matching rows are adjacent to each other and on the same node in the Transformer.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Use a Sort stage and let it generate the Sort Key Change column, which is exactly what you want.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Ahimsa
Premium Member
Premium Member
Posts: 78
Joined: Fri Aug 18, 2006 11:05 pm

Post by Ahimsa »

Thanks all for your response. I got it working.
This site is always a huge help.
joesat
Participant
Posts: 93
Joined: Wed Jun 20, 2007 2:12 am

Post by joesat »

I have a similar kind of requirement so I thought I'd ask here itself. Which stage variable should we give in the derivation for the Output Columns? That is, should the stage variable prefixed 'prev' be used or the one with 'curr' be used?

When I use 'prev' the first row is displayed with empty rows and when I use 'curr' the values are not displayed as expected...if it is inappropriate to continue in this topic, let me know..I will start a new topic with complete details...
Joel Satire
Post Reply