-3

I have two codes in the text file( 001 & 002) if row starts with 002 code the row amount should be added with the next 001 code row amount. The sequence should be repeated for each 001 code row. means every 001 code row amount should be the sum with the previous 002-row amount(i.e b/w each 001 code row the amount should be concatenated with next 001-row amount)

Actual rows from the input file

001 | 0.00

002 | 10.5

002 | 5.0

001 | 0.00

002 | 15.0

001 | 5

002 | 7

001 | 2

Expected output rows from the input file

001 | 0.00

002 | 10.5

002 | 5.0

001 | 15.5 ( adding amount with 2nd, 3rd-row amount)

002 | 15.0

001 | 20.0 (adding amount with previous 002 code amount)

002 | 7.0

001 | 9.0  (adding amount with previous 002 code amount)
Hadi
  • 36,233
  • 13
  • 65
  • 124
gopim
  • 35
  • 6
  • You didn't ask a question. You want to know how to do it? [Break your big problem into simpler problems](https://ericlippert.com/2014/03/21/find-a-simpler-problem/). – Dour High Arch May 16 '19 at 16:32
  • @Dour ,yes! I mentioned in the question subject – gopim May 16 '19 at 16:37
  • “How to achieve ...” is [not a question we can answer](https://meta.stackexchange.com/questions/226103/). I told you how to solve it in my comment. – Dour High Arch May 16 '19 at 16:39
  • 1
    Ok thanks,I will correct it in future. Can you tell me how do it? – gopim May 16 '19 at 16:50

1 Answers1

-1

Script Component (transformation): Mark Col1 as read only Mark Col2 as Read/Write

The trick here is that you need to store information outside of row processing so you need to declare variables outside of row processing.

Outside of Row Processing

//these are established on the first pass and used on every row moving forward.
decimal first002;
decimal last002;
int ctr=0;

inside row processing:

if(Row.Col1=="001")
{
    //Do something with COl2 based on ctr
    switch(ctr)
    { 
        case 0:
             break; //don't do anything
        case 1:
             Row.Col2 = first002;
             break;
        case 2:
             Row.Col2 = first002+last002;
             break;
        default:
             Row.Col2 += last002;
             break;
    }
}
else
{
   //this is a 002 and need to update variables.
   ctr++
   if(ctr==1)
   {first002 = Row.Col2;}
   else
   {last002 = Row.Col2;}
}
KeithL
  • 5,348
  • 3
  • 19
  • 25
  • 1
    Why was this downvoted? When arithmetic occurs across rows then a script component is probably needed in order to write the previous row's value to a variable so that the next row's value can be processed with the current row. Trying to do this in a query would be challenging, though the LEAD/LAG partition functions could help. – J Weezy May 17 '19 at 22:00
  • Well he used ints while the inputs are decimals. Doesn't handle nulls and adds more complexity and is slower than using a SQL based solution. – Milney May 19 '19 at 19:27
  • The original question used integers as sample data. that can easily be modified. – KeithL May 20 '19 at 12:49