Application Development and Automation 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: 

Collect Statement

thangeswaran
Explorer
0 Kudos
896

Hi Experts,

In internal table 'gt_item' I have got all the values by line item wise, client requirement to merge the feilds if the (vbeln-d,tline,steuc,cmpre) are same , for that I used the 'Collect' statement, the issue is 'GST' % also got merged, kindly provide the solution

LOOP AT gt_item INTO gs_item.


gs_item1-vbeln_d4 = gs_item-vbeln_d.
gs_item1-tline4 = gs_item-tline.
gs_item1-steuc4 = gs_item-steuc.
gs_item1-qty4 = gs_item-qty.
gs_item1-mtr4 = gs_item-mtr.
gs_item1-cmpre4 = gs_item-cmpre.
gs_item1-cgst4 = gs_item-cgst.
gs_item1-sgst4 = gs_item-sgst.
gs_item1-igst4 = gs_item-igst.
gs_item1-cgst_amt4 = gs_item-cgst_amt.
gs_item1-sgst_amt4 = gs_item-sgst_amt.
gs_item1-igst_amt4 = gs_item-igst_amt.

COLLECT gs_item1 INTO gt_item1.
CLEAR gs_item1.

ENDLOOP.

** Invioce has 10 line items each contains 2.5% cgst and 2.5% sgst

** The final result has attached below for your reference

8 REPLIES 8

FredericGirod
Active Contributor
798

Do you read the documentastion ?

abo
Active Contributor
798

As frdric.girod suggested, the documentation helps.

COLLECT will use all alphanumeric fields as key and sum all numeric fields, no questions asked. If you need finer control over either key or sum, then code it explicitly.

DominikTylczyn
SAP Champion
SAP Champion
798

Hello

As per SAP Help documentation of COLLECT statement:

This statement inserts the content of a work area wa either as a single row in an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same primary table key

Looks like CGST4 and SGST4 fields are numeric and they got summarized as described in the documentation. The COLLECT statement worked as expected.

What are you trying to achieve?

Best regards

Dominik Tylczynski

DominikTylczyn
SAP Champion
SAP Champion
798

Use CODE formatting when sharing ABAP code:

Sandra_Rossi
Active Contributor
798

Please edit your question, select your code and click the button below, otherwise it's a pain to read it:

LOOP AT gt_item INTO gs_item.

  gs_item1-vbeln_d4 = gs_item-vbeln_d.
  gs_item1-tline4 = gs_item-tline.
  gs_item1-steuc4 = gs_item-steuc.
  gs_item1-qty4 = gs_item-qty.
  gs_item1-mtr4 = gs_item-mtr.
  gs_item1-cmpre4 = gs_item-cmpre.
  gs_item1-cgst4 = gs_item-cgst.
  gs_item1-sgst4 = gs_item-sgst.
  gs_item1-igst4 = gs_item-igst.
  gs_item1-cgst_amt4 = gs_item-cgst_amt.
  gs_item1-sgst_amt4 = gs_item-sgst_amt.
  gs_item1-igst_amt4 = gs_item-igst_amt.

  COLLECT gs_item1 INTO gt_item1.
  CLEAR gs_item1.
ENDLOOP.

BH91976
Explorer
0 Kudos
798

As already mentioned by the others. Collect sums up all numerical values in the structure. But you might try something like this, to calculate only the fields necessary:

TYPES: BEGIN OF ty_tap,
         lgnum TYPE ltap-lgnum,
         tanum TYPE ltap-tanum,
         vsolm TYPE ltap-vsolm,
         brgew TYPE ltap-brgew,
       END OF ty_tap.

TYPES: ty_t_tap TYPE HASHED TABLE OF ty_tap WITH UNIQUE KEY lgnum tanum.
DATA: lt_result TYPE ty_t_tap.
SELECT lgnum, tanum, tapos, vsolm, brgew FROM ltap UP TO 1000 ROWS INTO TABLE @DATA(lt_ltap) WHERE lgnum = 101.
"Loop at group
LOOP AT lt_ltap ASSIGNING FIELD-SYMBOL(<fs_grp>) GROUP BY ( lgnum = <fs_grp>-lgnum
                                                            tanum = <fs_grp>-tanum
   ) ASCENDING REFERENCE INTO DATA(grp_ref).

  "Calculate subtotals
  DATA(ls_result) = REDUCE ty_tap( INIT result = VALUE ty_tap( lgnum = grp_ref->lgnum
                                                          tanum = grp_ref->tanum
                                                          vsolm = CONV ltap_vsolm( 0 )
                                                          brgew = CONV ltap_brgew( 0 ) )
                                  FOR ls_ltap IN lt_ltap WHERE ( lgnum = grp_ref->lgnum
                                                                 AND tanum = grp_ref->tanum )
                                     NEXT result-vsolm = result-vsolm + ls_ltap-vsolm
                                          result-brgew = result-brgew + ls_ltap-brgew
                                     ).
  "Append subtotals to new table
  INSERT ls_result INTO TABLE lt_result.
ENDLOOP.

BH91976
Explorer
0 Kudos
798

Or maybe even better, it might be that you have to add your sgs4 to the group by:

lt_result1 = VALUE #( FOR GROUPS grp OF ls_ltap IN lt_ltap GROUP BY ( lgnum = ls_ltap-lgnum
                                                                      tanum = ls_ltap-tanum ) ASCENDING
                      ( REDUCE ty_tap( INIT result = VALUE ty_tap( lgnum = grp-lgnum
                                                                   tanum = grp-tanum
                                                                   vsolm = CONV ltap_vsolm( 0 )                                                                       
                                                                   brgew = CONV ltap_brgew( 0 ) 
                                      )
                                      FOR ls_ltap1 IN lt_ltap WHERE ( lgnum = grp-lgnum 
                                                                      AND tanum    = grp-tanum )
                                            NEXT result-vsolm = result-vsolm + ls_ltap1-vsolm
                                                 result-brgew = result-brgew + ls_ltap1-brgew )                                             
                       )

                    ).

raymond_giuseppi
Active Contributor
0 Kudos
798

Either use up to date syntax as already posted (values/group by) or just define the target table as a sorted or hashed table with those numeric fields part of the primary key of the table.