‎2008 Dec 11 11:39 AM
Moderator message: please use an informative subject for your questions
Hi All,
I have an internal table with 2 fields h1 and h2. I need to have a count of entries in my internal table for each value of h1.
INTERNAL TABLE:
h1 h2
x1 1
x2 2
x1 1
x3 1
x2 3
RESULT:
h1 h2
x1 2
x2 2
x3 1
Please help...
Edited by: Matt on Dec 11, 2008 2:37 PM
‎2008 Dec 11 11:42 AM
Hi,
U can loop the internal table with the coditions like h1 = x1 then increment the count like that.
‎2008 Dec 11 11:42 AM
Hi Dinesh,
Sort your table
add one more field in the table like count and assign 1 to each of it and modify your table
then
loop at itab
use COLLECT key word ( this sums up your newly added count column based on first field)
endloop
Best Regards
Ramchander Rao.K
Edited by: ramchander krishnamraju on Dec 11, 2008 12:42 PM
‎2008 Dec 11 11:53 AM
HI ,
See the following pseudo code , might be helpful to you.
TYPES : BEGIN OF ty_itab,
h1(2),
h2 TYPE i,
count TYPE i,
END OF ty_itab.
DATA : it_itab TYPE TABLE OF ty_itab,
wa_itab TYPE ty_itab.
wa_itab-h1 = 'X1'.
wa_itab-h2 = 1.
wa_itab-count = 1.
APPEND wa_itab TO it_itab.
wa_itab-h1 = 'X2'.
wa_itab-h2 = 2.
wa_itab-count = 1.
APPEND wa_itab TO it_itab.
wa_itab-h1 = 'X1'.
wa_itab-h2 = 1.
wa_itab-count = 1.
COLLECT wa_itab INTO it_itab.
wa_itab-h1 = 'X3'.
wa_itab-h2 = 1.
wa_itab-count = 1.
APPEND wa_itab TO it_itab.
wa_itab-h1 = 'X2'.
wa_itab-h2 = 3.
wa_itab-count = 1.
COLLECT wa_itab INTO it_itab.
LOOP AT it_itab INTO wa_itab.
WRITE :/ wa_itab-h1 , wa_itab-h2 , wa_itab-count.
ENDLOOP.Thanks & Regards
‎2008 Dec 11 11:55 AM
Hi,
first sort the internal table by the first field h1.
then loop at itab. Use collect statment to sum the number of records for each h1 , for this create a second itab which has h1, as key field, and rec_count (this should be type i ) .
regards,
Advait
‎2008 Dec 15 8:39 AM
Could you please tell me how I can use the collect statement here
Use collect statment to sum the number of records for each h1 , for this create a second itab which has h1, as key field, and rec_count (this should be type i ) .
I have used it as COLLECT wa_itab into it_itab. but this doesnt seem to work!
Edited by: Dina Dinesh on Dec 15, 2008 9:39 AM
‎2008 Dec 15 8:55 AM
Hi
Try this out.
DATA : BEGIN OF itab OCCURS 0,
h1(2),
h2(1)," TYPE i,
COUNT TYPE I,
END OF itab.
itab-h1 = 'X1'.
itab-h2 = 1.
APPEND itab.
CLEAR itab.
itab-h1 = 'X2'.
itab-h2 = 2.
APPEND itab.
CLEAR itab.
itab-h1 = 'X1'.
itab-h2 = 1.
APPEND itab.
CLEAR itab.
itab-h1 = 'X3'.
itab-h2 = 1.
APPEND itab.
CLEAR itab.
itab-h1 = 'X2'.
itab-h2 = 3.
APPEND itab.
CLEAR itab.
itab-h1 = 'X1'.
itab-h2 = 1.
APPEND itab.
CLEAR itab.
SORT ITAB.
DATA COUNT TYPE I.
LOOP AT itab.
ON CHANGE OF ITAB-H1.
CLEAR COUNT.
ENDON.
COUNT = COUNT + 1.
ITAB-COUNT = COUNT.
MODIFY ITAB INDEX SY-TABIX.
ENDLOOP.
SORT ITAB BY H1 COUNT DESCENDING.
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING H1.
IF sy-subrc IS INITIAL.
loop at itab.
write 😕 itab-h1, itab-count.
endloop.
ENDIF.
if you still have any problem please comeup
Regards
Ram
Edited by: ramchander krishnamraju on Dec 15, 2008 9:57 AM
‎2008 Dec 15 10:13 AM
Hi
Source :
h1 h2
x1 1
x2 2
x1 1
x3 1
x2 3
I do not want to delete duplicates but display
it as result:
h1 h2 count
x1 1 2
x1 1 2
x2 2 2
x2 3 2
x3 1 1
I used the algorith you had suggested and got the result as
h1 h2 count
x1 1 1
x1 1 2
x2 2 1
x2 3 2
x3 1 1
Is there a possibility to get a maximum
of the count for each value of h1 and update the internal table
‎2008 Dec 17 1:46 PM
Hi,
Check this :
types : begin of t_table
h1(2) type c,
h2 type i,
end of t_table.
data gt_1 type standard table of t_table,
gt_2 type standard table of t_table with key h1,
gwa.
"fill data in the itab
"Sort the itab by h1.
sort gt_1 by h1.
"Collect the data into second table.
loop at gt_1 into gwa.
collect gwa into gt_2. "Since h1 is the key field in the gt_2, the collect will sum up for unique h1 values
endloop.
regards,
Advait
Edited by: Advait Gode on Dec 17, 2008 2:47 PM
‎2008 Dec 11 12:00 PM
Hi Dina,
The following snippet will work for your logic.
int_tab-Source table.
h1 h2
x1 1
x1 1
x2 2
x2 3
x3 1int_tab1-Target table.
h1 h2
x1 2
x2 2
x3 1SORT int_tab BY h1.
LOOP AT int_tab INTO wa_tab.
w_count = w_count + 1.
AT END OF h1.
wa_tab1-h1 = wa_tab-h1.
wa_tab1-h2 = w_count.
CLEAR w_count.
APPEND wa_tab1 TO int_tab1.
ENDAT.
ENDLOOP.Hope this will help you.
Regards,
Manoj Kumar P
‎2008 Dec 17 1:26 PM
Hi Manoj, I tried this but the AT END OF h1 doesnt seem to work correctly. Can you help?
‎2008 Dec 11 12:02 PM
sort itab by h1.
data: cnt type i.
loop at itab.
at end of h1.
read table itab index sy-tabix.
write itab-h1.
write cnt.
cnt = 0.
skip 1.
endat.
cnt = cnt + 1.
endloop.
‎2008 Dec 11 12:22 PM
Hi Dina,
As per my understanding you have to get the number of entries for each value of h1.
data : w_line type i value 1 .
sort it_itab ascending by h1.
LOOP AT it_itab INTO wa_itab.
at end of h1.
WRITE 😕 wa_itab-h1 ,w_line.
clear w_line.
endat.
w_line = w_line + 1 .
ENDLOOP.
it works,
Regards,
Aby.
‎2008 Dec 11 1:38 PM
‎2008 Dec 17 2:20 PM
Hi Dina,
Now I understood your requirement.
So your input is as follows:
Input:(int_tab)
h1 h2 count
x1 1 0
x2 2 0
x1 1 0
x3 1 0
x2 3 0Here is the logic:
SORT int_tab BY h1.
LOOP AT int_tab INTO wa_tab.
w_count = w_count + 1."Increment the count for each entry and clear at last
wa_tab-count = w_count.
MODIFY int_tab FROM wa_tab INDEX sy-tabix
TRANSPORTING count. "Change the existing count
AT END OF h1.
CLEAR w_count. "Clear the count at the end of each h1
ENDAT.
ENDLOOP.output:(int_tab)
h1 h2 count
x1 1 1
x1 1 2
x2 2 1
x2 3 2
x3 1 1Regards,
Manoj Kumar P
Edited by: Manoj Kumar on Dec 17, 2008 3:21 PM