‎2008 Jul 17 3:57 PM
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.
‎2008 Jul 17 4:01 PM
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
‎2008 Jul 17 3:59 PM
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.
‎2008 Jul 17 4:01 PM
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
‎2008 Jul 17 4:01 PM
HI,
LOOP AT ITAB TRANSPORTING NO FIELDS WHERE NAME CS 'SAP'.
ADD 1 TO COUNT.
ENDLOOP.
Regards,
Kiran.
‎2008 Jul 17 4:02 PM
@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.
‎2008 Jul 17 5:22 PM
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.