Page 1 of 1

how to get previous row value in current row

Posted: Thu Jun 14, 2012 2:15 am
by jhansi
source
id,sal
10,1000
20,2000
30,3000
40,5000

target
id,sal,pre_row_sal
10,1000,null
20,2000,1000
30,3000,2000
40,5000,3000

Re: how to get previous row value in current row

Posted: Thu Jun 14, 2012 2:48 am
by TPons
you can implement the logic using stage variables in Transformer stage

---------
Pons

Re: how to get previous row value in current row

Posted: Thu Jun 14, 2012 2:49 am
by TPons
you can implement the logic using stage variables in Transformer stage

---------
Pons

Re: how to get previous row value in current row

Posted: Thu Jun 14, 2012 3:16 am
by jhansi
can u explain me the logic? i did not get proper output with my logic
TPons wrote:you can implement the logic using stage variables in Transformer stage

---------
Pons

Posted: Thu Jun 14, 2012 3:49 am
by ray.wurlod
Did you search DSXchange? The technique of using stage variables to remember a value from the previous row has been explained on a number of occasions.

In short, though, the technique relies on the fact that stage variables are executed in the order (top down) in which they appear in the stage variables grid. So, for example, when the first stage variable is being executed for the current row, the second and subsequent stage variables still contain their value from the previous row.

Stage variables are initialized before the first row (per node) is processed, and not re-initialized after that.

Posted: Thu Jun 14, 2012 4:35 am
by jhansi
i got solution for this.but how to get next row value to current row?
ray.wurlod wrote:Did you search DSXchange? The technique of using stage variables to remember a value from the previous row has been explained on a number of occasions.
...

Posted: Thu Jun 14, 2012 4:50 am
by BI-RMA
jhansi wrote:i got solution for this.but how to get next row value to current row?
This is technically impossible, because you can't use a value in a row that DataStage has not read at the time you want to use it.

For many cases a simple solution may be to reverse the sort-order of your input and use the same technique as described above, though.

Posted: Thu Jun 14, 2012 4:57 am
by jhansi
actually my requirement is
id,sal,next_row_sal
10,1000,2000
20,2000,3000
30,3000,5000
40,5000,null

BI-RMA wrote:
jhansi wrote:i got solution for this.but how to get next row value to current row?
This is technically impossible, because you can't use a value in a row that DataStage has not read at the time you want to use it.

For many cases a simple solution may be to reverse the sort-order of your input and use the same technique as described above, though.

Posted: Thu Jun 14, 2012 5:47 am
by BI-RMA
Run your stream through two outputs of a transformer. Add a Dummy-column to both streams. Provide @Outrownum as the Derivation for one and @Outrownum -1 as the derivation to the other. Left Join the resulting streams with the Dummy column as Join-key. Rename the sal-column on the left-joined stream next_row_sal.

Check the result.

Posted: Thu Jun 14, 2012 6:49 am
by jhansi
how to left join those streams? i used datasets

Posted: Thu Jun 14, 2012 7:48 am
by BI-RMA
It does not matter where your data comes from or is written to.

You send the data from one source downstream through two output-links of the same transformer and left join both output-streams to each other.


DS --> TFM ===> Join --> DS

Posted: Thu Jun 14, 2012 9:17 am
by jhansi
but i am getting output as 1 2 3 4

Posted: Thu Jun 14, 2012 4:33 pm
by ray.wurlod
Four nodes? Four counts.

Posted: Mon Jun 18, 2012 2:14 am
by jhansi
but that is not my requirement...

Posted: Mon Jun 18, 2012 2:25 am
by ray.wurlod
Then don't run on four nodes.