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: 

need logic in loop

Former Member
0 Kudos

Hi Experts,

I need a logic in internal table loop.

Ex:

vbeln matnr werks quantity

1234 AAAA 0001 10.000

2345 AAAA 0001 20.000

2345 BBBB 0001 30.000

3456 AAAA 0001 35.000

I need a logic that for every new Material i need to know how many records are there in internal table for that material.

like...

sort itab ascending by matnr.

loop at itab.

at new matnr.

i need no: of records for that matnr ( AAAA) i.e. 3..

endloop.

i am confused where to use this at new...

can anyone provide the best logic in terns of performance?

Giri

1 ACCEPTED SOLUTION

former_member195698
Active Contributor
0 Kudos

One method with MATNR as the first field in the internal table

matnr vbeln werks quantity

AAAA 1234 0001 10.000

AAAA 2345 0001 20.000

BBBB 2345 0001 30.000

AAAA 3456 0001 35.000

one internal table with fields

MATNR COUNT


Sort tb_data by matnr.

loop at tb_data.

  at new matnr.
     clear wf_count.
  endat.

  wf_count = wf_count +1 .

  at end of matnr.

     tb_summary-matnr = tb_data-matnr.
     tb_summary-count  = wf_count.
     append tb_summary.
  endat.


endloop.

5 REPLIES 5

Former Member
0 Kudos

AT NEW won't help here.

Rob

former_member195698
Active Contributor
0 Kudos

One method with MATNR as the first field in the internal table

matnr vbeln werks quantity

AAAA 1234 0001 10.000

AAAA 2345 0001 20.000

BBBB 2345 0001 30.000

AAAA 3456 0001 35.000

one internal table with fields

MATNR COUNT


Sort tb_data by matnr.

loop at tb_data.

  at new matnr.
     clear wf_count.
  endat.

  wf_count = wf_count +1 .

  at end of matnr.

     tb_summary-matnr = tb_data-matnr.
     tb_summary-count  = wf_count.
     append tb_summary.
  endat.


endloop.

naimesh_patel
Active Contributor
0 Kudos

Since you have to get the number of material in the AT NEW, you should fill the number of material in some temporary table and than you it inside in the LOOP to read the material number.

Like:


DATA: BEGIN OF ITC OCCURS 0,
      MATNR TYPE MATNR,
      COUNT TYPE I.
      END OF ITC.

LOOP AT ITAB.
  ITC-MATNR = ITAB-MATNR.
  ITC-COUNT = 1.
  COLLECT ITC.
ENDLOOP.

loop at itab.

at new matnr.
READ TABLE ITC WITH KEY MATNR = ITAB-MATNR.
* ITC-COUNT  WILL GIVE THE NUMBER OF MATERIAL
ENDAT.

endloop.

Regards,

Naimesh Patel

0 Kudos

Guys,

any help on this?

what is the best solution for this....

Giri

0 Kudos

Hi Giri,

I think Naimesh Patels logic would work fine. Let us know if you have tried it. If yes, do the performance analysis and let us know.

Regards.

Vamshi.