2007 Dec 27 9:12 AM
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
2007 Dec 27 9:34 AM
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
2007 Dec 27 9:16 AM
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
2007 Dec 27 9:17 AM
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
2007 Dec 27 9:18 AM
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
2007 Dec 27 9:37 AM
hi nimesh ,
but where u are adding the weight of same materials?????
2007 Dec 27 9:47 AM
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
2007 Dec 27 9:27 AM
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.
2007 Dec 27 9:34 AM
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
2007 Dec 27 9:46 AM
like collect what all are the other statements related to internal tables????
like collect,append,insert etc
2007 Dec 27 9:54 AM
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