Application Development and Automation 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: 
Read only

Small Doubt with internal table

Former Member
0 Likes
614

Hi Guys,

I have an internal table like this :

1 a sdn

2 b sdn

3 c sdn

4 e sap

5 e sap

6 f sap

7 g sap

Now I want to get a count which will tell me that there are three lines with "sdn" and 4 lines with "sap".

Any idea how to do it?

Thanks

Kumar.

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
594

YOu can use the COLLECT to get the count.

Make an internal table, with 2 fields,

Count TYPE I

TEXT TYPE CHAR10.

Now,


LOOP AT ITAB.
  IT_SUM-COUNT = 1.
  IT_SUM-TEXT = ITAB-TEXT.
COLLECT IT_SUM.
CLEAR  IT_SUM.
ENDLOOP.

Regards,

Naimesh Patel

5 REPLIES 5
Read only

Former Member
0 Likes
594

itab { field1, field2}.

Sort itab by field 2.

count1, count2 type i.

Loop at itab,

at new field2.

if itab-field2 = 'sap'

count1 ++

else.

count2 ++

endif.

endat

endloop.

Read only

naimesh_patel
Active Contributor
0 Likes
595

YOu can use the COLLECT to get the count.

Make an internal table, with 2 fields,

Count TYPE I

TEXT TYPE CHAR10.

Now,


LOOP AT ITAB.
  IT_SUM-COUNT = 1.
  IT_SUM-TEXT = ITAB-TEXT.
COLLECT IT_SUM.
CLEAR  IT_SUM.
ENDLOOP.

Regards,

Naimesh Patel

Read only

Former Member
0 Likes
594

HI,

LOOP AT ITAB TRANSPORTING NO FIELDS WHERE NAME CS 'SAP'.

ADD 1 TO COUNT.

ENDLOOP.

Regards,

Kiran.

Read only

0 Likes
594

@Sekhar and Kiran

Here sap is just a value of the internal table and it may change to anything. So I cannot really hard-code the value into my programming.

Thanks

Kumar.

Read only

Former Member
0 Likes
594

That is not too difficult Kumar, if you paused to think for yourself how to implement the logic.

define itab2 similar to your itab.

copy contents of itab to itab2.

sort itab2 by field 2.

delete adjacent duplicates from itab2 comparing field 2.

sort itab1 by field2.

loop at itab2.

loop at itab1 where itab1-field2 = itab2-field2.

count++.

write count or store count in itab_count with one field- count & 2nd field value of itab2-field2 & append itab_count.

endloop.

refresh count.

endloop.