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: 

Regarding Control statement AT END OF...SUM

Former Member
0 Kudos

Hi

Please clarify me the below code to fine tune the program.

Code:------

  • MOVE lt_ekbe[] TO lt_ekbe1[].

lt_ekbe1[] = lt_ekbe[].

DELETE ADJACENT DUPLICATES FROM lt_ekbe COMPARING ebeln ebelp vgabe.

*For each EBELN, EBELP,VGABE combincation find the sum of DMBTR and WRBTR.

LOOP AT lt_ekbe INTO lw_ekbe.

LOOP AT lt_ekbe1 INTO lw_ekbe1

WHERE ebeln = lw_ekbe-ebeln AND

ebelp = lw_ekbe-ebelp AND

vgabe = lw_ekbe-vgabe.

l_wrbtr = l_wrbtr + lw_ekbe1-wrbtr.

l_dmbtr = l_dmbtr + lw_ekbe1-dmbtr.

MOVE-CORRESPONDING lw_ekbe1 TO lw_ekbe2.

lw_ekbe2-wrbtr = l_wrbtr.

lw_ekbe2-dmbtr = l_dmbtr.

ENDLOOP.

APPEND lw_ekbe2 TO lt_ekbe2.

ENDLOOP.

REFRESH:lt_ekbe,lt_ekbe1.

lt_ekbe[] = lt_ekbe2[].

REFRESH lt_ekbe2.

In above code two internal tables are used for summation of "wrbtr and dmbtr" fields. I'm thinking instead of that can we use SUM statement with in AT END OF, but again here based on "ebeln, ebelp, vgabe" we should sum the "wrbtr and dmbtr" values.

for ex: the records exist as below given, I wrote the code as below mentioned but the sum value is 0.00, kindly help me in this regard to avoid nested loops and multiple internal tables.

LOOP AT lt_ekbe INTO lw_ekbe.

AT END OF ebeln

AT END OF ebelp

AT END OF vgabe

SUM.

ENDAT.

ENDAT.

ENDAT.

ENDLOOP.

EBELN EBELP VGABE DMBTR WRBTR

4500000000|00100|1 | 50.00 | 50.00 |

4500000000|00100|1 | 50.00 | 50.00 |

4500000000|00100|1 | 50.00 | 50.00 |

4500000001|00100|1 | 200.00 | 200.00 |

4500000001|00100|2 | 200.00 | 200.00 |

4500000002|00100|1 | 100.00 | 100.00 |

4500000013|00010|1 | 3800.00 | 1000.00 |

4500000013|00010|1 | 3640.30 | 1000.00 |

4500000014|00100|1 | 10.00 | 10.00 |

4500000014|00100|2 | 10.00 | 10.00 |

4500000014|00200|1 | 20.00 | 20.00 |

4500000014|00200|2 | 20.00 | 20.00 |

Thanks & Regards

lasya

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Do like this,

Table it_ekbe1 of similar type it_ekbe.

SORT it_ekbe BY ebeln ebelp vgabe.

LOOP AT lt_ekbe INTO lw_ekbe.

AT END OF vgabe

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

ENDLOOP.

Hope it will solve ur problem.

Thanks

Azad.

Rewar if helpful.

6 REPLIES 6

Former Member
0 Kudos

Hi

see the sample code to use those events and do accordingly

All this AT NEW, AT FIRST, AT END OF and AT LAST are called control break statements of Internal tables and are used to calculate the TOTALS based on sertain key fields in that internal table

FIrst to use these statements the ITAB has to be sorted by the key fields on whcih you need the SUM of the fields.

Some time you will get * when mopving data from this int table to other table using these commands

so you have to use

READ TABLE ITAB INDEX SY-TABIX in AT..ENDAT..if you are using other fields between them

DATA: sflight_tab TYPE SORTED TABLE OF sflight

WITH UNIQUE KEY carrid connid fldate,

sflight_wa LIKE LINE OF sflight_tab.

SELECT *

FROM sflight

INTO TABLE sflight_tab.

LOOP AT sflight_tab INTO sflight_wa.

AT NEW connid.

WRITE: / sflight_wa-carrid,

sflight_wa-connid.

ULINE.

ENDAT.

WRITE: / sflight_wa-fldate,

sflight_wa-seatsocc.

AT END OF connid.

SUM.

ULINE.

WRITE: / 'Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

SKIP.

ENDAT.

AT END OF carrid.

SUM.

ULINE.

WRITE: / 'Carrier Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

NEW-PAGE.

ENDAT.

AT LAST.

SUM.

WRITE: / 'Overall Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

ENDAT.

ENDLOOP.

<b>Reward points for useful Answers</b>

Regards

Anji

Former Member
0 Kudos

Hi,

Do like this,

Table it_ekbe1 of similar type it_ekbe.

SORT it_ekbe BY ebeln ebelp vgabe.

LOOP AT lt_ekbe INTO lw_ekbe.

AT END OF vgabe

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

ENDLOOP.

Hope it will solve ur problem.

Thanks

Azad.

Rewar if helpful.

Former Member
0 Kudos

Hi, actually the control break statements are looking simple when we work with them we have to be very carefull. In the control break statements on which field we use the AT statement the right side fields will become * if those are Character values, and become 0 if those are integer values.

so follow this code it might be useful.

SORT it_ekbe BY ebeln ebelp vgabe.

LOOP AT lt_ekbe INTO lw_ekbe.

write:/ lw_ekbe-ebeln, lw_ekbe-ebelp, lw_ekbe-vgabe, lw_ekbe-dmbtr.

AT END OF vgabe

SUM.

write: / 30 lw_ekbe-dmbtr.

ENDAT.

ENDLOOP.

Message was edited by:

Pradeepkumar Ambala

Former Member
0 Kudos

hi

in two ways u can proceed.

1.. sort itab by 3 fields. and then find out the sum

SORT it_ekbe BY ebeln ebelp vgabe.

LOOP AT lt_ekbe INTO lw_ekbe.

AT END OF vgabe

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

ENDLOOP.

2. sort itab by ebeln ebelp vgabe.

SORT it_ekbe BY ebeln ebelp vgabe.

LOOP AT lt_ekbe INTO lw_ekbe.

AT END OF ebeln.

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

AT END OF ebelp.

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

AT END OF vgabe

SUM.

APPEND lw_ekbe TO it_ekbe1.

ENDAT.

at last

sum.

end at.

ENDLOOP

reward if useful

0 Kudos

Hi Azad/Pradeep/Vanilla

Still some confusion using Control statements. Pelase clarify me. I need one more help. I have internal table ITAB with matnr, werks, gjahr. I sorted the ITAB and deleted adjacent duplicates. Now I want only the highest year records based on matnr and plant.

Please find below code and data, how can i get the recent year records only. I gave both my input records and expecting output records.

SORT itab BY matnr werks gjahr DESCENDING.

DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr werks gjahr.

Input records

MATNR WERKS GJAHR

000000000100000023

M361

2007

000000000100000023

M361

2006

000000000100000023

M361

2005

000000000100000024

M361

2006

000000000100000024

M361

2005

000000000100000051

M007

2005

000000000100000054

G011

2005

000000000100000054

G011

2004

000000000100000054

M361

2005

000000000100000054

M361

2004

000000000100000055

G011

2005

|000000000100000055|G011 |2004 |

expecting output should be as below:

000000000100000023

M361

2007

000000000100000024

M361

2006

000000000100000051

M007

2005

000000000100000054

G011

2005

|000000000100000055|G011 |2005 |

Please help me in this regard

Thank you

Regards

lasya

Former Member
0 Kudos

Hi Azad/Pradeep/Venilla

appreciate your help, my problem got solved with your help. I gave points.

Thanks & Regards

lasya