2022 Nov 18 6:12 AM
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.
ENDLOOP.
** Invioce has 10 line items each contains 2.5% cgst and 2.5% sgst
** The final result has attached below for your reference
2022 Nov 18 6:56 AM
2022 Nov 18 7:09 AM
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.
2022 Nov 18 7:26 AM
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
2022 Nov 18 7:26 AM
2022 Nov 18 8:27 AM
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.
2022 Nov 18 2:16 PM
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.
2022 Nov 18 2:42 PM
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 )
)
).
2022 Nov 18 3:22 PM
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.