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: 

item wise sum

Former Member
0 Kudos
541

hi experts,

I have an itab where i have

delivno matnr weight

1 A 1000

1 A 500

1 B 2000

i need an output like this

delivno matnr weight

1 A 1500

1 B 2000

sort itab by delivno matnr.

loop at itab.

at new matnr.

sum.

write:/ itab-delivno,itab-matnr,itab-weight.

endat.

endloop.

but it is not working ..what mey be the reason ..

what shall i do to get the output.

regards,

mani

1 ACCEPTED SOLUTION

former_member402443
Contributor
0 Kudos
199

Hi Manik,

You can use the Collect Statement.

The collect statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key.

*So in your case the delivno matnr should be character*

and the weight field should be numeric.

and also the order of fields in the internal table should be like that

first character fields then all the numeric fields.

SELECT BUKRS LIFNR BELNR BLART WAERS WRBTR

FROM BSIK INTO CORRESPONDING FIELDS OF

TABLE ITAB FOR ALL ENTRIES IN FILETAB WHERE

BUKRS = FILETAB-BUKRS AND

LIFNR = FILETAB-LIFNR AND

BELNR = FILETAB-BELNR AND

BLART = 'KR'.

LOOP AT FILETAB.

LOOP AT ITAB WHERE LIFNR = FILETAB-LIFNR AND

BELNR = FILETAB-BELNR.

MOVE ITAB-BUKRS TO ITAB_FD-BUKRS.

MOVE ITAB-LIFNR TO ITAB_FD-LIFNR.

MOVE ITAB-BELNR TO ITAB_FD-BELNR.

MOVE ITAB-BLART TO ITAB_FD-BLART.

MOVE ITAB-WAERS TO ITAB_FD-WAERS.

MOVE ITAB-WRBTR TO ITAB_FD-WRBTR.

COLLECT ITAB_FD.

ENDLOOP.

Reward Points, if helpful.

Regards,

Manoj Kumar

9 REPLIES 9

Former Member
0 Kudos
199

Hi,

Check the following example:

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

Regards,

Bhaskar

Former Member
0 Kudos
199

U can use collect on this.

Loop at itab into wa_itab.

collect wa_itab into new_itab.

endloop.

Try this

  • Awrd Points if useful*

Bhupal

Nimesh_S_Patel
Explorer
0 Kudos
199

Hi Mani,

Yes,your code will erase value of del no in at new section.

Follow this -

Create new table same as your itab.

than,

Loop at itab.

move-correspondin itab to new tabel.

collect new table.

endloop.

and write this new table.

Hope it's clear now.

Regards

Nimesh S. Patel

0 Kudos
199

hi nimesh ,

but where u are adding the weight of same materials?????

0 Kudos
199

Hi Manik,

The Collect statement automatically sum the weight based on the material.

Taking your code example.

When you loop your internal table .

loop at itab .

collect itab."This statement aggregate the values so that on the based "of all the character fields all the numeric fields will be aggregated.

endloop.

Regards

Manoj Kumar

Former Member
0 Kudos
199

Hi Mani,

You have given the command SUM when AT NEW event is triggered. thats why you are not getting the output.

Put the SUM command in AT END <field name> event. Because the AT NEW event is triggered only when there is the end for the field. If the field ends then AT END event triggers and the system performs the summation.

sort itab by delivno matnr.

loop at itab.

AT END matnr.

sum.

write:/ itab-delivno,itab-matnr,itab-weight.

endat.

endloop.

REWARD IF USEFUL

reply for further clarifications.

regards,

Santosh.

former_member402443
Contributor
0 Kudos
200

Hi Manik,

You can use the Collect Statement.

The collect statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key.

*So in your case the delivno matnr should be character*

and the weight field should be numeric.

and also the order of fields in the internal table should be like that

first character fields then all the numeric fields.

SELECT BUKRS LIFNR BELNR BLART WAERS WRBTR

FROM BSIK INTO CORRESPONDING FIELDS OF

TABLE ITAB FOR ALL ENTRIES IN FILETAB WHERE

BUKRS = FILETAB-BUKRS AND

LIFNR = FILETAB-LIFNR AND

BELNR = FILETAB-BELNR AND

BLART = 'KR'.

LOOP AT FILETAB.

LOOP AT ITAB WHERE LIFNR = FILETAB-LIFNR AND

BELNR = FILETAB-BELNR.

MOVE ITAB-BUKRS TO ITAB_FD-BUKRS.

MOVE ITAB-LIFNR TO ITAB_FD-LIFNR.

MOVE ITAB-BELNR TO ITAB_FD-BELNR.

MOVE ITAB-BLART TO ITAB_FD-BLART.

MOVE ITAB-WAERS TO ITAB_FD-WAERS.

MOVE ITAB-WRBTR TO ITAB_FD-WRBTR.

COLLECT ITAB_FD.

ENDLOOP.

Reward Points, if helpful.

Regards,

Manoj Kumar

0 Kudos
199

like collect what all are the other statements related to internal tables????

like collect,append,insert etc

0 Kudos
199

Hi Manik,

The other statements related to the internal tables are

COLLECT, APPEND, INSERT, MODIFY,DELETE,

SORT,FIND IN , REPLACE IN, PROVIDE,READ ,

AT ENDAT,AT NEW , AT LAST, SUM AND SO ON

Regards

Manoj Kumar