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: 

loop at itab

Former Member
0 Kudos

LOOP AT imard ASSIGNING <ls_mard>.

LOOP AT is033 ASSIGNING <ls_s033>

where matnr = <ls_mard>-matnr

and lgort = <ls_mard>-lgort.

<b>here i get 4 rows and i want to take the highest date(sptag) mzubb

magbb for each matnr</b>and calc the menge.

ENDLOOP.

ENDLOOP.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Rani,

Is this OK.



REPORT zzarun_1.
DATA :imard TYPE STANDARD TABLE OF mard WITH HEADER LINE,
      is033 TYPE STANDARD TABLE OF s033 WITH HEADER LINE.

FIELD-SYMBOLS : <ls_mard> TYPE mard,
                <ls_s033> TYPE s033.

*Declare two Internal Tables
DATA : itemp  LIKE is033 OCCURS 0,  "Holding four records
       ifinal LIKE is033 OCCURS 0.  "Holding final data

LOOP AT imard ASSIGNING <ls_mard>.
  LOOP AT is033 ASSIGNING <ls_s033> WHERE matnr = <ls_mard>-matnr
                                      AND lgort = <ls_mard>-lgort.

*Substract here befor appending

    <b><ls_s033>-zlbkum =  <ls_s033>-end_qty - <ls_s033>-magbb.</b>

    APPEND <ls_s033> TO itemp.

  ENDLOOP.


*Take the row with highest date (sptag)
  SORT itemp BY sptag .
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest mzubb
  SORT itemp BY mzubb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest magbb
  SORT itemp BY magbb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

  "In internal table IFINAL you have the required data
  "Do your processing(Calculation)  here.


*After processing dont forget to refresh the Internal tables

  CLEAR   : itemp, ifinal.
  REFRESH : itemp, ifinal.

ENDLOOP.


Regards,

Arun S.

Message was edited by: Arun Sambargi

8 REPLIES 8

Former Member
0 Kudos

Hello,

sort itab by date in ascending order, read the first record.

Regards,

Vasavi.k

Former Member
0 Kudos

before doing this append another itab same structure has imard or is033 based on data mzubb magbb.. apend the itab.

sort the itab.based on sptag,,,and mzubb

LOOP AT imard ASSIGNING <ls_mard>.

LOOP AT is033 ASSIGNING <ls_s033>

where matnr = <ls_mard>-matnr

and lgort = <ls_mard>-lgort.

ENDLOOP.

ENDLOOP.

ferry_lianto
Active Contributor
0 Kudos

Hi Rani,

Please sort internal table IS033 with MATNR and LGORT (ASCENDING) and SPTAG, MZUBB and MAGBB (DESCENDING) prior to looping statement.

Then you use control break processing to get the highest date for SPTAG MZUBB and MAGBB when looping IS033.

AT NEW SPTAG.

...

ENDAT.

Hope this will give you an idea.

Regards,

Ferry Lianto

Former Member
0 Kudos

Hi Rani,

Is your requirement somenthing like this.

Consider this code.


REPORT zzarun_1.
DATA :imard TYPE STANDARD TABLE OF mard WITH HEADER LINE,
      is033 TYPE STANDARD TABLE OF s033 WITH HEADER LINE.

FIELD-SYMBOLS : <ls_mard> TYPE mard,
                <ls_s033> TYPE s033.

<b>*Declare two Internal Tables</b>
DATA : itemp  LIKE is033 OCCURS 0,  "Holding four records
       ifinal LIKE is033 OCCURS 0.  "Holding final data

LOOP AT imard ASSIGNING <ls_mard>.
  LOOP AT is033 ASSIGNING <ls_s033> WHERE matnr = <ls_mard>-matnr
                                      AND lgort = <ls_mard>-lgort.


    APPEND <ls_s033> TO itemp.

  ENDLOOP.


*Take the row with highest date (sptag)
  SORT itemp BY sptag .
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest mzubb
  SORT itemp BY mzubb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest magbb
  SORT itemp BY magbb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

  "In internal table IFINAL you have the required data
  "Do your processing(Calculation)  here.


<b>*After processing dont forget to refresh the Internal tables</b>

  CLEAR   : itemp, ifinal.
  REFRESH : itemp, ifinal.

ENDLOOP.

Regards,

Arun Sambargi.

0 Kudos

arun nice answer thsnks.

i need to update is033-zlbkum every line that i have in is033.

the calc is

zlbkum = end_qty - magbb.

Manohar2u
Active Contributor
0 Kudos

Can also try in this way, ( might possible )

sort is033 by sptag.
LOOP AT imard ASSIGNING <ls_mard>.
read table is033 with key marnt = matnr = <ls_mard>-matnr
and lgort = <ls_mard>-lgort.
if sy-subrc = 0.
you will get the first record which should be highest date.
endif.
endloop.

Regds

Manohar

Former Member
0 Kudos

Hi Rani,

Is this OK.



REPORT zzarun_1.
DATA :imard TYPE STANDARD TABLE OF mard WITH HEADER LINE,
      is033 TYPE STANDARD TABLE OF s033 WITH HEADER LINE.

FIELD-SYMBOLS : <ls_mard> TYPE mard,
                <ls_s033> TYPE s033.

*Declare two Internal Tables
DATA : itemp  LIKE is033 OCCURS 0,  "Holding four records
       ifinal LIKE is033 OCCURS 0.  "Holding final data

LOOP AT imard ASSIGNING <ls_mard>.
  LOOP AT is033 ASSIGNING <ls_s033> WHERE matnr = <ls_mard>-matnr
                                      AND lgort = <ls_mard>-lgort.

*Substract here befor appending

    <b><ls_s033>-zlbkum =  <ls_s033>-end_qty - <ls_s033>-magbb.</b>

    APPEND <ls_s033> TO itemp.

  ENDLOOP.


*Take the row with highest date (sptag)
  SORT itemp BY sptag .
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest mzubb
  SORT itemp BY mzubb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

*Take the row with highest magbb
  SORT itemp BY magbb.
  READ TABLE itemp ASSIGNING <ls_s033> INDEX 1.
  APPEND <ls_s033> TO ifinal.

  "In internal table IFINAL you have the required data
  "Do your processing(Calculation)  here.


*After processing dont forget to refresh the Internal tables

  CLEAR   : itemp, ifinal.
  REFRESH : itemp, ifinal.

ENDLOOP.


Regards,

Arun S.

Message was edited by: Arun Sambargi

0 Kudos

I know the <b>loop at where</b> statement has been improved quite a bit but for runtime considerations I would still suggest that you use a sorted table and use <b>read table with key</b> -- instead of the inner loop at itab where

(see one of the previous posts).

This might have very little impact if the table has just a few rows but can be quite different if each table has around 300,000 rows...

Enjoy