Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

calculate # of rows dynamically for an itab

flowers_candys
Participant
0 Kudos
111

Hello Gurus,

I have an internal table itab1:

col1 | col2 | col3 |

A | X | 123 |

A | | 456 |

A | X | 999 |

B | | 456 |

B | X | 999 |

C | X | 123 |

C | X | 456 |

C | X | 999 |

Now, I need to calculate values for A, B, C, or it could be anything...with respect to col2....

That is, currently,

for A with X, COUNT is 2 and

total COUNT for A is 3

for B with X, COUNT is 1 and

total COUNT for B is 2

for C with X, COUNT is 3 and

total COUNT for C is also 3

Now, since it is dynamic, I can not use describe, or loop using where clause as rows selection depends upon DB table..

Is it possible to use field symbol to capture these values dynamically...or

use AT END clause with FS...?

I dont know how to calculate the number of rows for a specific condition between at end and endat..

Kindly help in deciding the optimized approach....

Thanks in advance..

BR

Malhar

5 REPLIES 5

nabheetscn
Active Contributor
0 Kudos
83

Hi

You can sort on field1 and field2. You will have to use two end of statements at end of field1 will give total count for and at end of end of field2 will give count for unique combo of field1 and field2

Nabheet

0 Kudos
83

Ok..

sort itab by col1 col2.

loop at itab1.

at end of col1.

what statement will give me the actual count??

As I understand for summation, we just use SUM.

But, here for the actual count what to be used...?

endat.

at end of col2.

endat.

endloop.

BR

Malhar

0 Kudos
83

Or is it possible to achieve the same without using the at end of - endat (control break) stmnt and by using the Field Symbols..?

Please advixe me the same!!!

Thanks again..

0 Kudos
83

Keep local cariables one for total and one for x and count..on end of pass them...

Nabheet

Former Member
0 Kudos
83

create another 2 tables itab2, and itab3 with below fields.

col1|col2|col3==> make this of type char10|col4==>i have added col4 of type i.

loop at itab1 into is1.
 move corresponding is1 to is2. 
 clear is2-col3."==>we dont need this for count
 is2-col4 = 1.
 if is2-col2 = 'X'.
   collect is2 into ITAB2."==>for count with X
 endif.
 clear is2-col2.
 collect is2 into ITAB3. "===>for total count
 clear is2.
endloop.

output will be below

ITAB2

 col1  | col2 | col3 | col4|
 A     | X    |      |  2  |
 B     | X    |      |  1  |
 C     | X    |      |  3  |

ITAB3

 col1  | col2 | col3 | col4|
 A     |      |      |  3  |
 B     |      |      |  2  |
 C     |      |      |  3  |