Application Development and Automation 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: 
Read only

Internal table using At Statment

Former Member
0 Likes
1,002

Dear All

i have one internal table . In that table materialcode, qty,movementtype fields are available. i want to get sum of the qty based on materialcode and movermenttype.

internaltable

materialcode movementtype qty

1039-2006-mat 101 25

1039-2006-mat 101 25

1039-2006-mat 102 25

1039-2006-mat 102 25

1039-2006-mat 231 25

1039-2006-mat 231 25

cutpcs 101 25

cutpcs 102 25

i want output following

materialcode movementtype qty

1039-2006-mat 101 50

1039-2006-mat 102 50

1039-2006-mat 231 50

cutpcs 101 25

cutpcs 102 25

This is not ALV Report

Thanks and Regards

Suresh

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
973

suppose you have the data in itab then declare one int tbale itab1 like itab.

loop at itab.

read table itab1 with key matnr = itab-matnr bwart = itab-bwart.

if sy-subrc = 0.

itab1-menge = itab-menge + itab1-menge.

modify itab1 index sy-tabix.

else.

append itab to itab1.

endif.

clear : itab,itab1.

endloop.

loop at itab1.

write : / itab1-matnr,itab1-bwart,itab1-menge.

endloop.

regards

shiba dutta

9 REPLIES 9
Read only

Former Member
0 Likes
973

USE

AT NEW ur fields.

sum.

endat

Read only

Former Member
0 Likes
973

Hi se this example code you can understand very easily

  • Using AT FIRST , AT NEW, AT THE END OF , AT LAST.

DATA: BEGIN OF ITAB OCCURS 0,

F1 TYPE I,

F2(6) TYPE C,

F3(10) TYPE N,

F4(16) TYPE P DECIMALS 2,

END OF ITAB.

DATA: SUB_TOT(10) TYPE P DECIMALS 3.

**--1

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 30.

ITAB-F4 = '3000.00'.

APPEND ITAB.

CLEAR ITAB.

*--2

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

*-- 3

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

SORT ITAB BY F1.

LOOP AT ITAB.

AT FIRST.

WRITE: /35 ' MATERIAL DETAILS:'.

ULINE.

ENDAT.

AT NEW F1.

WRITE: / 'DETAILS OF MATERIAL:' COLOR 7 , ITAB-F1.

ULINE.

ENDAT.

WRITE: / ITAB-F1, ITAB-F2, ITAB-F3, ITAB-F4.

SUB_TOT = SUB_TOT + ITAB-F4.

AT END OF F1.

ULINE.

WRITE: / 'SUB TOTAL :' COLOR 3 INVERSE ON, SUB_TOT COLOR 3 INVERSE ON.

CLEAR SUB_TOT.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: 'SUM:', ITAB-F4.

ULINE.

ENDAT.

ENDLOOP.

Read only

0 Likes
973

hi Naresh Nelapatla

Thanks your Reply

Your code work first material and first movement type next not working

Regards

Suresh

Read only

former_member386202
Active Contributor
0 Likes
973

Hi,

For that u should use COLLECT statement

Loop at itab

collect itab to itab1.

endloop.

if its not working then

do like this

loop at itab.

lv_index = sy-index - 1.

read table itab with index lv_index

if sy-subrc = 0.

add ur amount here.

endif.

on change of material.

append itab1.

endon.

endloop.

Regards,

Prashant

Read only

Former Member
0 Likes
973

declare ur internal table in the following format. the field sequence should be as below

materialcode

movementtype

qty

After populating data. Sort as below.

sort itab by material code movementtype.

loop at itab.

at end of movementtype.

SUM.

write:/ itab-materialcode, itab-movementtype, itab-qty.

endat.

endlloop.

Read only

Former Member
0 Likes
973

Hi suresh,

try this.

data : store_mat(20) type c,

store_movt(4) type n,

ctr type i.

loop at itab.

at new matcode.

ctr = itab_qty.

store_mat = itab-matcode.

store_movt = itab-movt.

endat.

if sy-tabix ne 1.

if store_mat = itab-matcode and store_movt = itab-movt.

ctr = ctr + itab-qty.

else.

write : / itab-matcode,

/t itab-movt,

/t ctr.

clear itab.

clear ctr.

endif.

endif.

endloop.

hope this helps.

Read only

Former Member
0 Likes
973

<b>Collect</b>

Creating subtotal and groups of the internal table.

<b>Syntax of collect statement:</b>

Data: begin of itab occurs 0,

c1,

c2 type i,

end of itab.

itab-c1 = 'a'.

itab-c2 = 10.

<b>collect itab.</b>

itab-c1 = 'a'.

itab-c2 = 10.

<b>collect itab.</b>

loop at itab.

write:/ itab-c1,

itab-c2.

endloop.

Read only

0 Likes
973

hi

Thanks your Reply

Your code work first material and first movement type next not working

Regards

Suresh

Read only

Former Member
0 Likes
974

suppose you have the data in itab then declare one int tbale itab1 like itab.

loop at itab.

read table itab1 with key matnr = itab-matnr bwart = itab-bwart.

if sy-subrc = 0.

itab1-menge = itab-menge + itab1-menge.

modify itab1 index sy-tabix.

else.

append itab to itab1.

endif.

clear : itab,itab1.

endloop.

loop at itab1.

write : / itab1-matnr,itab1-bwart,itab1-menge.

endloop.

regards

shiba dutta