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

Internal tables

Former Member
0 Likes
2,300

Hello friends,

I have an internal table like the one below, now based upon the field f2, i need only the count i.e the no. of records in field f1,for instance there are 3 records for field f2 for value of 10 and similarly for 15 two records in f1. I tried using At new, at end of & on change on , but was not able to get the count of records.Please provide me a solution.Thanks in advance.

f1 f2

a 10

a 10

b 10

c 15

c 15

Thanks & Regards

Meenakshi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,273

wat u can do is get the records in the order F2 F1 in the same or another internal table ,then sort the table by F2 and then take the count based on AT END OF.

Hello friends,

I have an internal table like the one below, now based upon the field f2, i need only the count i.e the no. of records in field f1,for instance there are 3 records for field f2 for value of 10 and similarly for 15 two records in f1. I tried using At new, at end of & on change on , but was not able to get the count of records.Please provide me a solution.Thanks in advance.

f1 f2

a 10

a 10

b 10

c 15

c 15

Thanks & Regards

Meenakshi

15 REPLIES 15
Read only

Former Member
0 Likes
2,274

wat u can do is get the records in the order F2 F1 in the same or another internal table ,then sort the table by F2 and then take the count based on AT END OF.

Read only

0 Likes
2,273

Hi:

here is sample code. hope it will help u.

try ur filed f2 to keep at left. meanz it should be ur ist field

REPORT ztn_test.

data: BEGIN OF it_tab occurs 10,
      f2 type i,
      f1(1),
      END OF it_tab .

data: wa_it_tab like it_tab.


 it_tab-f2 = '10'.  it_tab-f1 = 'A'. APPEND it_tab.
 it_tab-f2 = '10'.  it_tab-f1 = 'A'. APPEND it_tab.
 it_tab-f2 = '12'.  it_tab-f1 = 'B'. APPEND it_tab.
 it_tab-f2 = '12'.  it_tab-f1 = 'B'. APPEND it_tab.
 it_tab-f2 = '12'.  it_tab-f1 = 'C'. APPEND it_tab.
 it_tab-f2 = '11'.  it_tab-f1 = 'D'. APPEND it_tab.
 SORT it_tab by f2.

 data: count TYPE i.
 loop AT it_tab into wa_it_tab.
   at new f2.
      count = 0.
   ENDAT.

   count = count + 1.

   at end of f2.
     write:/ wa_it_tab-f2 , 'are', count.
   ENDAT.
   CLEAR wa_it_tab.
endloop.

Read only

Former Member
0 Likes
2,273

Hi,

Try this way..

Sort itab by F2.
Loop at itab.

  Count = count + 1.
  At end of F2.
    Write:/ itab-f2, Count.
    clear count.
  Endat.

Endloop.

Read only

valter_oliveira
Active Contributor
0 Likes
2,273

Hello.

For that, you can use collect statement as follows:


TYPES: begin of ty_sum,
         f2 LIKE <your_itab>-f2,
         sum TYPE i,
       end of ty_sum.

DATA: it_sum TYPE STANDARD TABLE OF ty_sum,
      wa_sum TYPE ty_sum.

LOOP AT <your_itab> INTO <your_wa>.
  CLEAR wa_sum.
  wa_sum-f2 = <your_wa>-f2
  wa_sum-sum = 1.
  COLLECT wa_sum INTO it_sum.
ENDLOOP.

Regards,

Valter Oliveira.

Read only

Former Member
0 Likes
2,273

Hi,

you can do this using the same logic as you are using, but one simple change you need to do is

1.Create an internal table with fileds f2 and f1

2.Sort the internal table according to f2

3.and simply use AT NEW and AT END, with the count

Hope this will solve the issue.

Pooja

Read only

Former Member
0 Likes
2,273

hi meenakshi

sort the table with field f2.

set flag .then declare a temporary varaible say v1.loop at itab.

store the value of f2 filed in temp variable comparing previous value.

Read only

Former Member
0 Likes
2,273

try something like this..

data :

v1 type i value 0,

count type i value 0,

flag type i value 0.

sort itab by f2.

loop at itab.

if v1 = itab-f2 or flag = 0.

flag = 1.

count = count + 1.

at endof f2.

write : count.

endat.

v1 = itab-f2.

else.

flag = 1.

clear : count.

count =count + 1.

v1 = itab-f2.

endloop.

Edited by: rakesh on Feb 2, 2009 12:12 AM

Read only

Pranil1
Participant
0 Likes
2,273

Hi Meenakshi,

Instead of using at end of field name use ON CHANGE-ENDON inside loop. and create one extra field in internal table as count and asssign this count value to it.. and then clear count variable.

Hope it will help.

Regards,

Pranil.

Read only

Former Member
0 Likes
2,273

hi try this code.

data: BEGIN OF it_tab occurs 10,

f2 type c,

f1 type string,

END OF it_tab .

data: BEGIN OF it_tab1 occurs 10,

f2 type c,

f1 type string,

END OF it_tab1 .

it_tab-f2 = '10'. it_tab-f1 = 'A'. APPEND it_tab.

it_tab-f2 = '10'. it_tab-f1 = 'A'. APPEND it_tab.

it_tab-f2 = '15'. it_tab-f1 = 'B'. APPEND it_tab.

it_tab-f2 = '15'. it_tab-f1 = 'B'. APPEND it_tab.

it_tab-f2 = '14'. it_tab-f1 = 'C'. APPEND it_tab.

it_tab-f2 = '11'. it_tab-f1 = 'D'. APPEND it_tab.

SORT it_tab by f2.

DATA: itab like STANDARD TABLE OF it_tab,

results TYPE match_result_tab.

it_tab1[ ] = it_tab[ ].

DATA x type string.

data: mcnt type i.

loop at it_tab.

CONCATENATE it_tab-f1 x into x.

endloop.

DELETE ADJACENT DUPLICATES FROM it_tab1 COMPARING f1.

LOOP at it_tab1.

FIND ALL OCCURRENCES OF it_tab1-f1

IN x

MATCH COUNT mcnt.

write: mcnt , it_tab1-f1.

endloop.

Edited by: abap on Feb 2, 2009 6:45 AM

Edited by: abap on Feb 2, 2009 6:46 AM

Read only

Former Member
0 Likes
2,273

Hi,

You can do this way:

Create another internal table with only field f2.

types: begin of t_itab1,

f1 type c,

f2 type i,

end of t_itab1.

types: begin of t_itab2,

f2 type i,

end of t_itab2.

Data: i_itab1 type table of t_itab1,

wa_itab1 like line of i_itab1,

i_itab2 type table of t_itab2,

wa_itab2 like line of i_itab2.

Data: l_counter type i.

sort i_itab1 by f2.

loop at i_itab1 into wa_itab1.

wa_itab2-f2 = wa_itab1-f2.

append wa_itab2 to i_itab2.

endloop.

Delete adjacent duplicates from i_itab2.

loop at i_itab2 into wa_itab2.

refresh l_counter.

loop at i_itab1 into wa_itab1 where wa_itab1-f2 = wa_itab2-f2.

l_counter = l_counter + 1.

endloop.

write: / wa_itab2-f2, l_counter.

endloop.

Read only

Former Member
0 Likes
2,273

Hi,

You can use AT END OF that field name,

then put SELECT Count on that field name.

Hope it helps you

Mansi

Read only

former_member784222
Active Participant
0 Likes
2,273

Hi,

Have another table with the following structure:

data :begin of tab occurs 0,
  f1 type c,
  f2(10) type c
  count type i,
end of tab.

loop at first_internal table.
  tab-f1 = first_internal_table-f1.
  tab-f2 = first_internal_table-f2.
  count = 1.
  collect tab.
  clear: tab.
endloop.

So TAB will have the count.

regards,

S. Chandramouli.

Edited by: Chandramouli Subburathinam on Feb 2, 2009 11:57 AM

Hi Valter, I didnt notice your reply. Collect statement will surely work. But data type of F2 should be type N or type C, because if it is P or I Collect statement will sum field F2 also

Read only

Former Member
0 Likes
2,273

Hi,

please do tike this.............

TYPES : BEGIN OF t_tab ,

f2 TYPE i,

f1(1),

END OF t_tab .

DATA:i_tab TYPE STANDARD TABLE OF t_tab,

wa_tab TYPE t_tab.

wa_tab-f2 = '10'. wa_tab-f1 = 'A'. APPEND wa_tab TO i_tab.

wa_tab-f2 = '10'. wa_tab-f1 = 'A'. APPEND wa_tab TO i_tab.

wa_tab-f2 = '10'. wa_tab-f1 = 'B'. APPEND wa_tab TO i_tab.

wa_tab-f2 = '15'. wa_tab-f1 = 'C'. APPEND wa_tab TO i_tab.

wa_tab-f2 = '15'. wa_tab-f1 = 'C'. APPEND wa_tab TO i_tab.

SORT i_tab BY f2.

DATA: count TYPE i.

LOOP AT i_tab INTO wa_tab.

AT NEW f2.

count = 0.

ENDAT.

count = count + 1.

AT END OF f2.

WRITE:/ wa_tab-f2 , 'count', count.

ENDAT.

CLEAR wa_tab.

ENDLOOP.

Read only

Former Member
0 Likes
2,273

Hi Meenakshi,

Try it this way:

Data:
  count type i value 1,
  w_f2  type i.

Data:
  Begin of wa,
    f1(10) type c,
    f2     type i,
  End of wa.

Data:
  itab like table of wa.

Clear wa.
wa-f1 = 'a'.
wa-f2 = 10.
Append wa to itab.

Clear wa.
wa-f1 = 'a'.
wa-f2 = 10.
Append wa to itab.

Clear wa.
wa-f1 = 'b'.
wa-f2 = 10.
Append wa to itab.

Clear wa.
wa-f1 = 'c'.
wa-f2 = 12.
Append wa to itab.

Clear wa.
wa-f1 = 'c'.
wa-f2 = 12.
Append wa to itab.

Clear wa.
wa-f1 = 'c'.
wa-f2 = 12.
Append wa to itab.

Clear wa.
wa-f1 = 'c'.
wa-f2 = 15.
Append wa to itab.

Clear wa.
wa-f1 = 'c'.
wa-f2 = 15.
Append wa to itab.

Sort itab by f2.

Loop at itab into wa.

  At First.
    Count = 0.
    w_f2  = 0.
  Endat.

  If w_f2 eq wa-f2.
    Count = count + 1.
    w_f2 = wa-f2.
  Elseif w_f2 ne wa-f2.
    Write:/ count, w_f2.
    w_f2 = wa-f2.
    count = 1.
  Endif.

  At last.
    Write:/ count, w_f2.
  Endat.

Endloop.

With luck,

Pritam.

Read only

Former Member
0 Likes
2,273

useful