Hi
Hope this helps - I've not tested it but it should work you just need to declare the count variables.
Kind regards
Andy
SORT itab BY col1 col2 LOOP AT itab. IF itab-col2 = '00'. count_0 = count_0 + 1. ELSEIF itab-col2 = '01'. count_1 = count_0 + 1. AT END col1. WRITE: /itab-col1, count_1, count_2. count_0 = 0. count_1 = 0. ENDAT. ENDLOOP.
Hi,
follow below logic.
sort itab by col1.
data:begin of itab1 occurs 0 ,
col1(2) type c,
count00 type i,
count01 type i,
end of itab1.
loop at itab.
at new col1.
clear lv_cnt,lv_cnt1.
endat.
if itab-col2 = '00'
lv_cnt = lv_cnt + 1.
elseif itab-col2 = '01'
lv_cnt1 = lv_cnt1 + 1.
endif.
. at end of col1.
itab1-col1 = itab-col1.
itab1-count00 = lv_cnt.
itab1-count01 = lv_cnt1.
append itab1.
clear itab1.
endat.
endloop.
loop at itab1.
write:itab1.
endloop.
itab1 will have output
AA 2 1
BB 1 2
Regards
amole
check this code it may help u.
report z_example.
data:begin of itab occurs 0,
col1(3) type c,
col2(3) type c,
end of itab.
data:begin of itab1 occurs 0,
col1(3) type c,
count1 type i,
count2 type i,
end of itab1.
itab-col1 = 'AA'.
itab-col2 = '01'.
append itab.
itab-col1 = 'AA'.
itab-col2 = '00'.
append itab.
itab-col1 = 'AA'.
itab-col2 = '01'.
append itab.
itab-col1 = 'BB'.
itab-col2 = '01'.
append itab.
itab-col1 = 'BB'.
itab-col2 = '00'.
append itab.
sort itab by col1.
loop at itab.
at new col1.
itab1-col1 = itab-col1.
endat.
if itab-col2 = '01'.
itab1-count1 = itab1-count1 + 1.
elseif itab-col2 = '00'.
itab1-count2 = itab1-count2 + 1.
endif.
at end of col1.
append itab1.
clear itab1.
endat.
endloop.
loop at itab1.
write:/ itab1-col1,itab1-count1,itab1-count2.
endloop.
Regards
Hi,
Below is the source code for this scenario.
Maintain swap internal table and also Counts internal table as shown in the below source code.
report abc.
data:
v_count type i,
v_tabix type sy-tabix.
*-- Actual Data
data:begin of it_actual occurs 0,
f1(3) type c,
f2(3) type c,
end of it_actual.
*-- Swap Actual Data
data:begin of it_swap occurs 0,
f1(3) type c,
f2(3) type c,
end of it_swap.
*-- Holds Counts for sorted data
data:begin of it_sort occurs 0,
col1(3) type c,
col2(3) type c,
count type i,
end of it_sort.
start-of-selection.
*-- Append Test Data
perform append.
*-- Sort Data
perform sort.
&----
*& Form append
&----
text
----
--> p1 text
<-- p2 text
----
FORM append .
it_actual-f1 = 'AA'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'AA'.
it_actual-f2 = '00'.
append it_actual.
it_actual-f1 = 'AA'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '00'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '01'.
append it_actual.
it_actual-f1 = 'BB'.
it_actual-f2 = '00'.
append it_actual.
*-- Your actual format
loop at it_actual.
write:/ it_actual-f1, it_actual-f2.
endloop.
skip 2.
*-- Populate other table which swaps the fields
loop at it_actual.
it_swap-f1 = it_actual-f2.
it_swap-f2 = it_actual-f1.
append it_swap.
clear it_swap.
endloop.
ENDFORM. " append
&----
*& Form sort
&----
text
----
--> p1 text
<-- p2 text
----
FORM sort .
*-- Sort as per requirement
sort it_swap by f1 f2.
loop at it_swap.
write:/ it_swap-f1, it_swap-f2.
endloop.
loop at it_swap.
*-- Populate Loop index
v_tabix = sy-tabix.
at new f2.
*-- Initialize counter
clear v_count.
endat.
*-- Add counter
v_count = v_count + 1.
at end of f2.
*-- Read to avoid STARS
read table it_swap index v_tabix.
*-- Append other table
it_sort-col1 = it_swap-f2.
it_sort-col2 = it_swap-f1.
it_sort-count = v_count.
append it_sort.
clear it_sort.
endat.
endloop.
*-- Now you have the below Internal table which holds counts
skip 2.
loop at it_sort.
write: / it_sort-col1, it_sort-col2, it_sort-count.
endloop.
*-- You can sort this again to have the proper output
skip 2.
sort it_sort by col1 col2.
loop at it_sort.
v_tabix = sy-tabix.
at new col1.
write: / it_sort-col1.
endat.
at end of col2.
read table it_sort index v_tabix.
write: it_sort-count.
endat.
endloop.
ENDFORM. " sort
define itab with fields col1 cnt0 cnt1.
itable->is the internal table with your data.
sort itable.
Loop at itable.
at new col1.
clear : lcnt0, lcnt1.
endat.
if itable-col2 = '00'.
add 1 to lcnt0.
elseif itable-col2 = '01'.
add 1 to lcnt1.
endif.
at end of col1.
move itable-col1 to itab-col1.
move lcnt0 to itab-cnt0.
move lcnt1 to itab-cnt1.
append itab.
endat.
Endloop.
itab has the data you require
Hi,
data:begin of itab occurs 0,
cl1(2) type c,
cl2(2) type c,
end of itab.
data:begin of itab2 occurs 0,
cl1(2) type c,
ca0 type i,
ca1 type i,
end of itab2.
data:ca0 type i value 0,ca1 type i value 0,
count type i value 0.
itab-cl1 = 'AA'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'AA'.
itab-cl2 = '00'.
append itab.
itab-cl1 = 'AA'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '00'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '01'.
append itab.
itab-cl1 = 'BB'.
itab-cl2 = '00'.
append itab.
<b>sort itab.
loop at itab.
on change of itab-cl1.
clear: ca0,ca1,count.
endon.
if itab-cl2 = '00'.
ca0 = ca0 + 1.
else.
ca1 = ca1 + 1.
endif.
perform outdata.
endloop.</b>
&----
*& Form outdata
&----
text
----
--> p1 text
<-- p2 text
----
form outdata .
if count > 0.
if itab2-cl1 <> itab-cl1.
itab2-cl1 = itab-cl1.
itab2-ca0 = ca0.
itab2-ca1 = ca1.
append itab2.
else.
itab2-cl1 = itab-cl1.
itab2-ca0 = ca0.
itab2-ca1 = ca1.
append itab2.
write:/ itab2-cl1,itab2-ca0,itab2-ca1.
endif.
endif.
count = count + 1.
endform. " outdata
Please do reward points if it solves your problem
Regards,
Sowjanya
or in simple way without using any control breaks
loop at itab.
if itab-cl1 = 'AA'.
if itab-cl2 = '00'.
ca0 = ca0 + 1.
else.
ca1 = ca1 + 1.
endif.
else.
if itab-cl2 = '00'.
cb0 = cb0 + 1.
else.
cb1 = cb1 + 1.
endif.
endif.
endloop.
Regards,
Sowjanya
Add a comment