Skip to Content
author's profile photo Former Member
Former Member

Can we use collect

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Aug 29, 2007 at 08:53 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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**

  • author's profile photo Former Member
    Former Member
    Posted on Aug 29, 2007 at 07:53 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 29, 2007 at 09:26 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.