Application Development 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: 

at new

Former Member
0 Kudos

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.

6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

When using the at NEW statment, the field in the AT NEW statement should be the first field in the itab. If you are not getting the required results, this may be the reason for it.

REgards,

RIch Heilman

0 Kudos

if i write on change instead of at new ?

0 Kudos

That would work too. Make sure that itab is sorted by that field and check sy-tabix > 1, because you probably don't want to do something the first time thru the loop.

Regards,

Rich Heilman

Former Member
0 Kudos

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

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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

Former Member
0 Kudos

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