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: 

Can we use collect

Former Member
0 Kudos

Hi,

The structure of an internal table say i_tab consists of material number(matnr), PO number(ebeln), PO item number(ebelp), PO Qty of type QUANT and BOM qty of type QUANT.

In the internal table i_tab, for the same material number, PO number and PO item, we may have more than one record in the internal table.

In that case we need to add up the numerical quantities(PO qty and BOM qty) into one single record and delete the duplicate records.

We should have only one record for the same material number, PO number and PO item number with the values of the numerical quantities added up from the duplicate ones…

How do we do that..Can we use collect. Pls give a code snippet for the same.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Rahul,

You can not have the same combination for PO#, PO item# and the material number. Always the PO Item# will change for a PO.

I mean to say, In any document, you cannot have two rows with Item number..

But, you can have the same material in multiple Item rows.. Normally under these cases, we add the up the quantities for the same material in the first occurence of that material.

For ex..

PO PO Item Matnr Qty

11111 00001 mat1 10

11111 00002 mat2 20

11111 00003 mat1 15

In this case, after the collection, the data should be

PO PO Item Matnr Qty

11111 00001 mat1 25

11111 00002 mat2 20

Code like below:

LOOP AT tab1.

tab2 = tab1.

CLEAR tab2-itemno.

COLLECT tab2.

ENDLOOP.

SORT tab1 BY PO# matnr itemno.

LOOP AT tab2.

READ TABLE tab1 WITH KEY PO# = tab2-PO#

matnr = tab2-matnr.

IF sy-subrc EQ 0.

tab2-itemno = tab1-itemno.

MODIFY tab2.

ENDIF.

ENDLOOP.

Thanks and Best Regards,

Vikas Bittera.

**Points for useful answers**

9 REPLIES 9

Former Member
0 Kudos

HI,

yes you can use collect. Matnr, ebeln and ebelp are character fields so we can use them as keys to add quantity fields. There will be only one row for each set of data so there is no need for deleting duplicate entries.

Regards,

Mallick

0 Kudos

Can u pls give the code snippet for the same.

Thanks,

0 Kudos

TYPES: begin of t_tab,

MATNR,

EBELN,

EBELP,

POQUANT,

BOQUANT,

END OF t_tab.

DATA: i_tab type table of t_tab,

i_final type table of t_tab.

DATA wa_tab LIKE LINE OF i_tab,

*Now Select data in I_TAB

WA_TAB1 LIKE LINE OF I_FINAL.

LOOP AT ITAB into WA_TAB.

MOVE WA_TAB INTO WA_TAB1.

collect wa_tab1 into i_final.

CLEAR: WA_TAB, WA_TAB1.

ENDLOOP.

Former Member
0 Kudos

Hi,

Yes you can use the collect statment below is the example code fro your help. I am just giving lofic here not correct in syntax.

TYPES: begin of t_tab,

MATNR,

EBELN,

EBELP,

POQUANT,

BOQUANT,

END OF t_tab.

DATA: i_tab type table of t_tab,

i_final type table of t_tab.

DATA wa_tab LIKE LINE OF i_tab.

*Now Select data in I_TAB

LOOP AT ITAB into WA_TAB.

collect wa_tab into i_final.

ENDLOOP.

  • THis code will do what you want.

Hope its clear.

<b>Reward points if its helpfull.</b>

Thanks and Regards

Tanweer

0 Kudos

thanks..

But this even adds up for the record which does not have any duplicate entries.

Former Member
0 Kudos

Hi Rahul,

You can not have the same combination for PO#, PO item# and the material number. Always the PO Item# will change for a PO.

I mean to say, In any document, you cannot have two rows with Item number..

But, you can have the same material in multiple Item rows.. Normally under these cases, we add the up the quantities for the same material in the first occurence of that material.

For ex..

PO PO Item Matnr Qty

11111 00001 mat1 10

11111 00002 mat2 20

11111 00003 mat1 15

In this case, after the collection, the data should be

PO PO Item Matnr Qty

11111 00001 mat1 25

11111 00002 mat2 20

Code like below:

LOOP AT tab1.

tab2 = tab1.

CLEAR tab2-itemno.

COLLECT tab2.

ENDLOOP.

SORT tab1 BY PO# matnr itemno.

LOOP AT tab2.

READ TABLE tab1 WITH KEY PO# = tab2-PO#

matnr = tab2-matnr.

IF sy-subrc EQ 0.

tab2-itemno = tab1-itemno.

MODIFY tab2.

ENDIF.

ENDLOOP.

Thanks and Best Regards,

Vikas Bittera.

**Points for useful answers**

0 Kudos

Hi Vikas,

Thanks a lot.

For the same PO number, PO item and material number, we may get duplicate entries based on several delivery schedules.

Let me explain the problem in this case. We are fetching the data from RESB table.

So after fetching the data from RESB we may or may not get duplicate entries which is based on whether the PO is having several delivery schedules or not.

So the internal table will have entries which are not duplicated for the one set of same PO number, POitem number and material number and it may duplicate entries for other set of same PO number but different PO item number and material number.

For ex the internal table after fetching data from RESB will be like as given below:

PO number PO item no Matno PO qty BOM qty

454555555 0010 10000002 100 20

454555555 0010 10000003 100 10

454555555 0020 10000002 100 10

454555555 0020 10000002 100 10

454555555 0020 10000003 100 20

The PO number is same, but for the item 0020 the material no 10000002 gets repeated twice..In this case we need to have only one entry for 10000002 and both the PO qty and BOM qty should be added up.

The internal table should be like this after the modification:

PO number PO item no Matno PO qty BOM qty

454555555 0010 10000002 100 20

454555555 0010 10000003 100 10

<u>454555555 0020 10000002 200 20</u>

454555555 0020 10000003 100 20

You can see one single entry for the material 1000002 for item 0020 instead of two and both the quantities added up.

The internal table which is used here is without a header line.

Can u pls give the code snippet for the same by using the collect statement.

Former Member
0 Kudos

HI

YES YOU CAN USE THE COLECT STATEMENT

<b>COLLECT wa INTO itab [result].</b>

This 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. As of Release 6.10, you can use result to set a reference to the inserted or changed row in the form of a field symbol or data reference.

Prerequisite for the use of this statement is that wa is compatible with the row type of itab and all components that are not part of the table key must have a numeric data type (i, p, f).

In standard tables that are only filled using COLLECT, the entry is determined by a temporarily created hash administration. The workload is independent of the number of entries in the table. The hash administration is temporary and is generally invalidated when the table is accessed for changing. If further COLLECT statements are entered after an invalidation, a linear search of all table rows is performed. The workload for this search increases in a linear fashion in relation to the number of entries.

In sorted tables, the entry is determined using a binary search. The workload has a logarithmic relationship to the number of entries in the table.

In hashed tables, the entry is determined using the hash administration of the table and is always independent of the number of table entries.

If no line is found with an identical key, a row is inserted as described below, and filled with the content of wa:

In standard tables the line is appended.

In sorted tables, the new line is inserted in the sort sequence of the internal table according to its key values, and the table index of subsequent rows is increased by 1.

In hashed tables, the new row is inserted into the internal table by the hash administration, according to its key values.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

COLLECT should only be used if you want to create an internal table that is genuinely unique or compressed. In this case, COLLECT can greatly benefit performance. If uniqueness or compression are not required, or the uniqueness is guaranteed for other reasons, the INSERT statement should be used instead.

The use of COLLECT for standard tables is obsolete. COLLECT should primarily be used for hashed tables, as these have a unique table key and a stable hash administration.

If a standard table is filled using COLLECT, it should not be edited using any other statement with the exception of MODIFY. If the latter is used with the addition TRANSPORTING, you must ensure that no key fields are changed. This is the only way to guarantee that the table entries are always unique and compressed, and that the COLLECT statement functions correctly and benefits performance. The function module ABL_TABLE_HASH_STATE can be used to check whether a standard table is suitable for editing using COLLECT.

Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc

FROM sflight

INTO seats.

COLLECT seats INTO seats_tab.

ENDSELECT.

reward if usefull<b></b>

Former Member
0 Kudos

Hi rahul

Yes yoou can use collect but in ur case it will be a peformance issue

so try avoiding it and use append asn modify wherever applicable

Thnaks

vivekanand