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

Hi,

While declaring the Internal Table mention MATNR,EBELN,EBELP AS Key . Like,

Data : Begin of wa,

......

......

End of wa,

itab like TABLE OF wa with KEY MATNR EBELN EBELP.

After the SELECT Query,Write

LOOP AT ITAB INTO WA.

COLLECT WA INTO ITAB.

ENDLOOP.

Regards,

Padmam.

18 REPLIES 18

Former Member
0 Kudos

MATNR

EBELN

EBELP

POQTY

BOQTY

yes...you can easily use collect in this scenario....the functionality of collect statement is such that it considers all non-numeric fields of the table as the key and based on that it sums up the numeric fields. As long as you have matnr, ebeln and ebelp as character fields, you can use collect to sum up poqty and boqty.

0 Kudos

Can you give the code snippet for that.

Thanks

Former Member

Hi,

While declaring the Internal Table mention MATNR,EBELN,EBELP AS Key . Like,

Data : Begin of wa,

......

......

End of wa,

itab like TABLE OF wa with KEY MATNR EBELN EBELP.

After the SELECT Query,Write

LOOP AT ITAB INTO WA.

COLLECT WA INTO ITAB.

ENDLOOP.

Regards,

Padmam.

0 Kudos

But its also adding up the qty values for the matnr, ebeln, ebelp which is having only one record.

0 Kudos

what are the data types for these fields? they need to be non numeric...not even type N

0 Kudos

Hi,

Try this,

Data : Begin of wa,

......

......

End of wa,

itab like TABLE OF wa with KEY MATNR EBELN EBELP.

After the SELECT Query,Write

LOOP AT ITAB INTO WA.

READ TABLE ITAB WITH KEY MATNR = WA-MATNR EBELN = WA-EBELN

EBELP = WA-EBELP.

IF SY-SUBRC = 0.

COLLECT WA INTO ITAB.

ENDIF.

ENDLOOP.

Regards,

Padmam.

0 Kudos

Its working fine when the internal table is having duplicate entries for the same matnr, ebeln, ebelp and rspos.

But suppose the first record doesn't have any duplicate entries, but still the qty values gets updated..

So what condition can we put so that we can check whether the first record is having duplicate entries before updating thru collect..

Thank you so much..

0 Kudos

Hi Rahul,

That Statement will work Fine ,Rahul.

After the Loop is entered, it will check for Duplicate Entries

in that Internal Table Using READ Statement.

If Present means,it will collect it.Otherwise it will exit the if loop.

You just check the SY-SUBRC values without giving Loop & by giving Values.

ie.,

READ TABLE ITAB WITH KEY MATNR = <matnr> EBELN = <pur ord no> EBELP = <pur item no>.

U Just enter this code & see for SY-SUBRC using Debugger.

If it is ok means u can continue with that code itself.

Regards,

Padmam.

0 Kudos

Hi Padmam,

Below is the code I’m using.. Pls validate.

Fp_i_resb is an internal table without a header line.

loop at fp_i_resb into l_wa_resb.

read table fp_i_resb into l_wa_resb1 with key matnr = l_wa_resb-matnr

ebeln = l_wa_resb-ebeln

ebelp = l_wa_resb-ebelp

rspos = l_wa_resb-rspos .

if sy-subrc eq 0.

collect l_wa_resb into fp_i_resb.

endif.

clear: l_wa_resb, l_wa_resb1.

endloop.

sort fp_i_resb by matnr.

Delete adjacent duplicates from fp_i_resb comparing matnr ebeln ebelp rspos.

For the first record, condition becomes true and execute the collect statement even though there is only one entry.

Pls check and confirm.

Thanks.

0 Kudos

Hi,

Will u pls Paste ur entire coding here.Then only it will be easy for me to Check it.

Thanks & Regards,

Padmam.

0 Kudos

Hi,

I'm pasting the entire subroutine.

Pls check.

FORM get_eket_rseb USING fp_i_ekko_ekpo TYPE ty_t_ekko_ekpo

CHANGING fp_i_eket TYPE ty_t_eket

fp_i_resb TYPE ty_t_resb.

IF NOT fp_i_ekko_ekpo[] IS INITIAL.

data: l_wa_resb TYPE ty_resb,

l_wa_resb1 TYPE ty_resb.

SELECT rsnum

FROM eket

INTO TABLE fp_i_eket

FOR ALL ENTRIES IN fp_i_ekko_ekpo

WHERE ebeln = fp_i_ekko_ekpo-ebeln

AND ebelp = fp_i_ekko_ekpo-ebelp.

*No need to check sy-subrc as it always gives components.

ENDIF.

  • Retrieve data from Reservation Rqmt

  • sort

SORT fp_i_eket BY rsnum.

IF NOT fp_i_eket[] IS INITIAL.

SELECT

rsnum "requirements

rspos " Item Number of Reservation / Dependent Requirements

matnr "Material number

werks "Plant

charg "Batch

BDMNG "Requirements quantity "Mod by rahul

meins "Base unit of measure "Mod by Rhaul

*Begin of mod-012/MoD-015 " IM5185751

erfmg "Quanity in entry UOM

erfme "Entry unit of measure

  • End of mod-012/MOD-015 " IM5185751

baugr "Pegged Requirement

bwart "Movement type

stlty "BOM Category

stlnr "Bill of material

stlkn "BOM item node number

stpoz "Internal counter

ebeln "PO

ebelp "PO item

stlal "Alternative BOM

ebele "mod by Rahul

FROM resb

INTO TABLE fp_i_resb

FOR ALL ENTRIES IN fp_i_eket

WHERE

  • begin of mod-001

rsnum = fp_i_eket-rsnum AND

werks IN s_plant.

  • end of mod-001

  • If data retrieval fails display error message

IF sy-subrc <> 0.

MESSAGE i451.

" No Reservation/Dependent data found for the given criteria

LEAVE LIST-PROCESSING.

ENDIF.

loop at fp_i_resb into l_wa_resb.

read table fp_i_resb into l_wa_resb1 with key matnr = l_wa_resb-matnr

ebeln = l_wa_resb-ebeln

ebelp = l_wa_resb-ebelp

rspos = l_wa_resb-rspos .

if sy-subrc eq 0.

collect l_wa_resb1 into fp_i_resb.

endif.

clear: l_wa_resb, l_wa_resb1.

endloop.

sort fp_i_resb by matnr.

Delete adjacent duplicates from fp_i_resb comparing matnr ebeln ebelp rspos.

ENDIF.

ENDFORM. " get_eket_rseb

0 Kudos

Hi,

Have u mentioned rspos as key while declaring the internal table?

Will pls give me the Internal Table Structure.

There is no need of this Statement.Because collect statement will add the numeric values & delete that record.

sort fp_i_resb by matnr.

Delete adjacent duplicates from fp_i_resb comparing matnr ebeln ebelp rspos.

Regards,

Padmam.

0 Kudos

Hi,

the internal table structure is where the key fields in the table resb are rsnum and rspos. Do we need to use that same key in the read statemnt

TYPES: BEGIN OF ty_resb,

rsnum TYPE rsnum, "Number of reservation requirements

rspos TYPE rspos, " Item Number of Reservation / Dependent

" Requirements

matnr TYPE matnr, "Material number

werks TYPE werks_d, "plant

charg TYPE charg_d,

bdmng TYPE bdmng, "Requirements quantity

meins TYPE meins, "Base unit of measure

  • Begin of Mod-006/MOD-007/MOD-008 " IM5185751

erfmg TYPE erfmg, "Quantity in Entry UOM

erfme TYPE erfme, "Entry unit of measure

  • End of Mod-006/MOD-007/MOD-008 " IM5185751

baugr TYPE baugr, "Material number of higher-levelassembly

bwart TYPE bwart, "Movement Type

stlty TYPE stlty, "BOM Category

stlnr TYPE stnum, "Bill of material

stlkn TYPE stlkn, "BOM item node number

stpoz TYPE cim_count, "Internal counter

ebeln TYPE ebeln, "Purchasing Document Number

ebelp TYPE ebelp, "Item Number of Purchasing Document

stlal TYPE stlal, "Alternative BOM

ebele TYPE eeten,

scrap_quan TYPE kmpmg,

END OF ty_resb.

0 Kudos

Hi Padmam,

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.

0 Kudos

Hi Rahul,

Use the below code:

DATA : e_tab TYPE ty_tab1. (Declare a structure of same type as internal table)

DATA : tab2 TYPE STANDARD TABLE of ty_tab1. (Declare second table)

LOOP AT tab1 INTO e_tab.

COLLECT e_tab INTO tab2.

ENDLOOP.

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

0 Kudos

Hi Vikas,

When using collect the numercal fields of internal table should be of type i , p or f.

The numerical fields of my internal table are of type QUANT..

so how do we proceed.

0 Kudos

Hi Rahul,

I forgot to tell.. i am sorry, for CURR fields, it works automatically..

In your second internal table.. Declare the field quantity as below:

TYPES : BEGIN OF ty_tab2,

xx TYPE ....,

yy TYPE ....,

quantity TYPE p DECIMALS 2,

END OF ty_tab2.

DATA : tab2 TYPE STANDARD TABLE OF ty_tab2.

DATA : e_tab2 TYPE ty_tab2.

Now, in your LOOP,

LOOP AT tab1 INTO e_tab.

MOVE-CORRESPONDING e_tab TO e_tab2.

e_tab2-qunatity = tab1-(quantity field name).

COLLECT e_tab2 INTO tab2.

ENDLOOP.

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Former Member
0 Kudos

its always better in such scenario to use DELETE ADJACENT DUPLICATE because collect statement adds all the numerical fields deleting all the character fields.

If you want to enter the value into internal table with out duplicate entries then below two methods (internal table with out header line) second is better in performance.

1) LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-field1 TO gs_itab1-field1.

APPEND gs_itab1 TO gt_itab1.

ENDLOOP.

DELETE ADJACENT DUPLICATES FROM gt_itab1 COMPARING field1.

2) LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-field1 TO gs_itab1-field1.

COLLECT gs_itab1 INTO gt_itab1.

ENDLOOP.

the same for character is

TYPES: BEGIN OF gty_type,

num(3) TYPE c,

END OF gty_type.

DATA: gt_itab TYPE TABLE OF gty_type,

gs_itab TYPE gty_type,

gt_itab1 TYPE TABLE OF gty_type,

gs_itab1 TYPE gty_type,

gt_itab2 TYPE TABLE OF gty_type,

gs_itab2 TYPE gty_type.

gs_itab-num = 'ABC'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'DEF'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'DEF'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'GHI'.

APPEND gs_itab TO gt_itab.

gs_itab-num = 'GHI'.

APPEND gs_itab TO gt_itab.

  • Insert values to table gt_itab1

CLEAR: gs_itab, gs_itab1.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab1-num.

APPEND gs_itab1 TO gt_itab1.

CLEAR: gs_itab, gs_itab1.

ENDLOOP.

  • Delete duplicate values from gt_itab1

DELETE ADJACENT DUPLICATES FROM gt_itab1 COMPARING num.

  • Insert only unique values into table gt_itab2

CLEAR: gs_itab, gs_itab2.

LOOP AT gt_itab INTO gs_itab.

MOVE gs_itab-num TO gs_itab2-num.

COLLECT gs_itab2 INTO gt_itab2.

CLEAR: gs_itab, gs_itab2.

ENDLOOP.

WRITE: 'GT_ITAB'.

LOOP AT gt_itab INTO gs_itab.

WRITE: / gs_itab-num.

ENDLOOP.

WRITE: / 'GT_ITAB1'.

LOOP AT gt_itab1 INTO gs_itab1.

WRITE: / gs_itab1-num.

ENDLOOP.

WRITE: / 'GT_ITAB2'.

LOOP AT gt_itab2 INTO gs_itab2.

WRITE: / gs_itab2-num.

ENDLOOP.

reward point if helpful