08-21-2006 6:32 PM
Table zinfo Entries:
Id name sales sales2
Field Type C C P P
a1 Smith 100.00 50.00
a1 Jones 100.00 50.00
a2 Bob 100.00 50.00
a3 Bob 100.00 50.00
a4 Mike 100.00 50.00
a5 Mary 100.00 50.00
a5 Mary 100.00 50.00
Using the above information, what is the result of the following code?
Loop at zinfo.
At new name.
Sum.
Write: / zinfo-id, zinfo-name, zinfo-sales.
Endat.
Endloop.
08-21-2006 6:36 PM
08-21-2006 6:43 PM
08-21-2006 6:48 PM
08-21-2006 6:51 PM
Hi,
No diff.Use on change for getting the sume . see the last piece of block of code.
data: begin of itab occurs 0 ,
id(2) type c,
name(50) type c,
sales type p,
sales2 type p,
end of itab.
itab-id = 'a1'.
itab-name = 'Smith'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a1'.
itab-name = 'Jones'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a2'.
itab-name = 'Bob'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a3'.
itab-name = 'Bob'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a4'.
itab-name = 'Mike'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a5'.
itab-name = 'Mary'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
itab-id = 'a5'.
itab-name = 'Mary'.
itab-sales = '100.00'.
itab-sales2 = '50.00'.
append itab.
<b>*Without Sort</b>
Loop at itab.
<b>At new name.</b>
Sum.
Write: / itab-id, itab-name, itab-sales.
Endat.
Endloop.
uline.
<b>*With Sort
sort itab by id name ascending.</b>
Loop at itab.
<b>At new name.</b>
Sum.
Write: / itab-id, itab-name, itab-sales.
Endat.
Endloop.
*
uline.
*using on change of with combination of sort
Loop at itab.
<b>on change of itab-name.</b>
Sum.
Write: / itab-id, itab-name, itab-sales.
endon.
Endloop.
Regards,
Raghav
08-21-2006 7:00 PM
You can use the COLLECT statement also, to collect into another ITAB.
report zrich_0001.
data: begin of itab occurs 0,
id(2) type c,
name(10) type c,
sales type p decimals 2,
sales2 type p decimals 2,
end of itab.
data: begin of itab2 occurs 0,
name(10) type c,
sales type p decimals 2,
sales2 type p decimals 2,
end of itab2.
itab-id = 'A1'.
itab-name = 'Smith'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A1'.
itab-name = 'Jones'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A2'.
itab-name = 'Bob'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A3'.
itab-name = 'Bob'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A4'.
itab-name = 'Mike'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A5'.
itab-name = 'Mary'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
itab-id = 'A5'.
itab-name = 'Mary'.
itab-sales = '100'.
itab-sales2 = '50'.
append itab.
loop at itab.
move-corresponding itab to itab2.
collect itab2 .
endloop.
loop at itab2.
write:/ itab2-name, itab2-sales, itab2-sales2.
endloop.
Regards,
Rich Heilman
08-21-2006 7:39 PM
Please remember that it is important to SORT your internal table b4 using ON CHANGE or AT NEW events.
Id name sales sales2
Field Type C C P P
a1 Smith 100.00 50.00
a1 Jones 100.00 50.00
a2 Bob 100.00 50.00
a3 Bob 100.00 50.00
a4 Mike 100.00 50.00
a5 Mary 100.00 50.00
a5 Mary 100.00 50.00
Using the above information, what is the result of the following code?
Loop at zinfo.
At new name.
Sum.
Write: / zinfo-id, zinfo-name, zinfo-sales.
Endat.
Endloop.
As for your example...the output would be as follows...
A1 Smith 100.00
A1 Jones 100.00
A2 Bob 100.00
A3 Bob 100.00
A4 Mike 100.00
A5 Mary 200.00
The way AT NEW event works is that it is triggered whenever the data of the internal table till Name field changes, so in the above case even if the id changes and name remains the same. The difference between ON CHANGE and AT is that ON CHANGE would only trigger if the name field changes...so for the above data using ON CHANGE even would result in the below output.
A1 Smith 100.00
A1 Jones 100.00
A2 Bob 200.00
A4 Mike 100.00
A5 Mary 200.00
Regards
Anurag