# How to achieve the below scenario in abinitio

Input file has below contents:
Ball Run
1 1
2 1
3 1
4 1
5 1
6 1
1 0
2 1
3 1
4 1
5 1
6 1
1 1
2 1
3 1
4 1
5 1
6 0
Required Output :
1 6
2 5
3 5

#### vinoth

• Jan 26th, 2015

I can solve this with use of a SCAN followed with rollup.

to just get the over details scan component with NULL as key and below transform.

```Codetype temporary_type=record
decimal(1000) count;
end; /*Temporary variable*/
temp :: initialize(in) =
begin
temp.count :: 0;
end;

temp :: scan(temp, in) =
begin
temp.count :: if(in.ball==1) temp.count+1 else temp.count;
end;

out :: finalize(temp, in) =
begin
out.over :: temp.count;
out.run :: in.run;
end;

then "over" as key in rollup
type temporary_type=record
decimal(100) sumup;
end; /*Temporary variable*/
temp :: initialize(in) =
begin
temp.sumup :: 0;
end;

temp :: rollup(temp, in) =
begin
temp.sumup :: temp.sumup+in.run;
end;

out :: finalize(temp, in) =
begin
out.run :: temp.sumup;
out.over :: in.over;
end;
```

There may be a simple solution but this is what I can think of :)

• Feb 2nd, 2015

Use Key change Function in Rollup . Key change function allows us t create group in different ways if we dont have a grouping key. The rule is as long as the function returns 0 all records are considered in the same group. A new group starts when the function returns 1.
out :: key_change(in1,in2)=
begin
out :: if (in.ball == 1) 1 else 0;
end;
Global Variable
------------------
let decimal("x01") count = 0;
Temporary Variable
------------------------
decimal("x01") total_runs;
INITIALIZE
--------------
temp.total_runs :: 0;
Rollup Function
-------------------
temp.total_runs :: temp.total_runs + in.runs;
Finalize
---------
count= count+1;
out.overs :: count;
out.runs_per_over :: temp.total_runs;

#### naman gupta

• Feb 16th, 2015

What if this scenario is as follows:
ball run
1 1
2 1
3 2
4 2
5 1
6 3
7 2
8 1
9 0
10 1
11 0
12 3
and required output is
over run
1 10
2 7

#### sum321 Profile Answers by sum321

• Feb 22nd, 2015

Hi Naman,

Just use the below key_change function to solve your problem..

```Codeout :: key_change(in1,in2)=
begin
out :: in2.ball%6 == 1;
end;```

#### Gaurav

• Mar 18th, 2015

In such case, it is much easier if you devide the first field with 6 and get quotient value as key for rollup.

#### AbInitio Beginner

• Mar 14th, 2017

Can you explain how does the scan work here! u have given the condition as " if (in.ball==1) temp.count+1 else temp.count" and when the count is increased to 2 how does it work inside the "if" condition. Here what would be the answer after this scan function? Please help me to understand this logic!

#### Aravind Anilkumar

• May 23rd, 2017

Use Rollup in expanded mode. Key Idea is the key change function in rollup.

#### Viswajit Nayak

• Jun 17th, 2017

Use the code below in rollup component.
Solution:
Input File --->Rollup---->Output file

#### Srivani

• Jul 10th, 2017

By using Rollup key change function we can achieve this

#### jyotiprasad Profile Answers by jyotiprasad

• Sep 7th, 2017

input-->rollup(key change)-->op
Rollup
type temporary_type=
record
decimal("
")run_per_over;
end;
out :: key_change(in1, in2) =
begin
out :: if((decimal("|"))in2.ball % 6==1)1 else 0;
end;
temp :: initialize(in) =
begin
temp.run_per_over ::0;
end;
temp ::rollup(temp,in)=
begin
temp.run_per_over :: temp.run_per_over +(decimal("|"))in.run;
end;
out :: finalize(temp, in) =
begin
out.ball ::(decimal("|"))next_in_sequence();
out.run ::temp.run_per_over;
end;

#### Abinitio Expert

• Sep 7th, 2017

Yes roll up key change is the correct answer

#### pranau

• Jan 22nd, 2018

using single rool-up:
key change=> in.ball%6==1
out::temporary_type(in)
let decimal (5) sum;
out::initialize(temp)
temp.sum=0;
out::rollup(in,temp)
temp.sum=temp.sum+in.run;
finalize
out.over::next_in_sequence();
out.Total_run::temp.sum;

#### PJ

• Apr 17th, 2018

We can do this Single Rollup and also with very minimal coding as below.
out :: rollup(in) =
begin
out.over :: next_in_sequence();
out.runs :: sum(in.runs);
end;
out :: key_change(in1, in2) =
begin
out :: if(in2.ball % 6 == 1) 1 else 0;
end;

#### Jatin

• Apr 30th, 2018

The Simplest approach with use of component and no coding.
considering ball as first column and runs as second columns
1. Use Reformat and create variable seq_no and store value of next_in_sequence().
2. In the same reformat calculate one column seq = (seq_no-ball)+1
3. Connect Rollup with input must be sorted and key seq and calculate sum(runs) as Total_runs
and use next_in_sequence as Overs.

#### Shubham

• Sep 28th, 2018

Hi,
A beginner level approach would be as described below:
step1- We can use a reformat after input file component and in that we can use a variable to give sequence to all the records using next_in_sequence() function. By doing this we will have a sequence generated from1-18 in this case
step2. then in the transformations tab we can directly map run field to the output and we can create an overs field there where we will use if else condition based on sequence(eg. if(seq is from 1-6)1 else if(seq is from 7-12)2 else 3. So we will have 3 sets of overs(6 1s,6 2s and 6 3s).
step3. We can use a rollup with key(overs) and do a sum(runs) and map overs and runs to the output.
That is it. We will get the required output.
Thanks!
There are number of ways to approach a given problem. The above case is what I could think of immediately after reading the scenario

#### Ashu

• Nov 5th, 2018

Use Reformat then Use Rollup
Refortmat Transformation :
let decimal("|") Gover=1;
let string("|") OverChange="N";
out :: reformat(in) =
begin
Gover = if (OverChange == "Y") Gover+1 else Gover;
OverChange=if (next_in_sequence()%6 == 0) "Y" else "N";
out.* :: in.*;
out.Over :: Gover;
end;
Then Roll up on Over and sum(run)

#### MJ

• Sep 12th, 2019

It will give wrong result. You should use "in2.ball%6==0"  