Four Stars

Remember value from previous row?

Hi,
is it possible to remember a value from the previous row? I have a time period where I inserted a new value and need the value from the previous (or next depending on the sorting) row.
id beg_date end_date info
--+--------+--------+----
1 20060101 99991231 abcd
now I insert the changed info for id 1
id beg_date end_date info
--+--------+--------+----
1 20070101 99991231 xyz
1 20060101 99991231 abcd
Then I want to enddate the previous entry with the beg_date of the new entry
id beg_date end_date info
--+--------+--------+----
1 20070101 99991231 xyz
1 20060101 20070101 abcd

Can sombody tel me how to preserve the previous value in Talend?
3 REPLIES
Four Stars

Re: Remember value from previous row?

Hi,
Thanks for the solotion. I did some minor changes and now it works. I changed the #start part into
my $oldComponentColumnValue='';
my $componentColumnValue='99991231';

and added a filter in the tMap becourse it gives 1 extra row with only the previous date.
Employee

Re: Remember value from previous row?

Ok that's great.
Just a litte point, with your filter in the tMap, if you want the last beg_date, you have to make the same test in the loop of the tPerlFlex.
if (tPerlFlex_1 ne '') {
$oldComponentColumnValue = $componentColumnValue;
$componentColumnValue = $tPerlFlex_1;
}
Four Stars

Re: Remember value from previous row?

Hi,
Thanks for the hint on the condition. I did some further experimenting with some extra grouping on id. If the data is sorted on id and beg_date descending then I can use the following code in tPerlRow and can skip the filter in the tMap component:
# start part of your Perl code
my $maxBegDate = 99991231;
my $oldBegDate = null;
my $currentBegDate = $maxBegDate;
my $oldId = null;
my $currentId = 0;
# here is the main part of the component,
# a piece of code executed in the row
# loop
$oldId = $currentId;
$currentId = $tPerlFlex_1;
$oldBegDate = $oldId != $currentId ? $maxBegDate : $currentBegDate;
$currentBegDate = $tPerlFlex_1;

Now I have the previous value for beg_date per id group.