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: 

collect statement using in report

ahanadasgupta_23
Explorer
0 Kudos

Hi All,

Suppose, I want to use collect statement for matnr values and according to that I want sum of quantity field. but I get an error that we can use collect command if all non unique key fields are numeric. How to write the code for that Please anyone guide me.

Thank you in advance.

1 ACCEPTED SOLUTION

hagit
Active Participant
0 Kudos

Did you use ‘with EMPTY KEY’ ?

If yes then you have to replace it – “WITH KEY <name of the non numeric field>”

For example replace line 18 with line 19:

TYPES:
BEGIN OF sty,
ch(3) TYPE c,
int TYPE i.
TYPES:END OF sty .
TYPES:
tty_orig TYPE TABLE OF sty
* ,tty_sum TYPE TABLE OF sty with EMPTY KEY. "line 18
,tty_sum TYPE TABLE OF sty WITH KEY ch . "line 19


DATA: lt_orig TYPE tty_orig
,ls_orig LIKE LINE OF lt_orig
,lt_sum TYPE tty_sum
.

ls_orig-ch = 'a'.
ls_orig-int = 1.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'a'.
ls_orig-int = 3.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'b'.
ls_orig-int = 11.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'b'.
ls_orig-int = 33.
APPEND ls_orig TO lt_orig.

clear ls_orig.
LOOP AT lt_orig INTO ls_orig .
COLLECT ls_orig INTO lt_sum.
ENDLOOP.
5 REPLIES 5

venkateswaran_k
Active Contributor

Please share your internal table on which you set a loop - to collect the sum of quantities.

Also share the piece of code on whihc you do collect.

Prasobh
Advisor
Advisor
0 Kudos

Key should be unique to perform the collect statement. You can use loops on your internal table, and write the logic for addition or use SUM.

https://blogs.sap.com/2018/06/15/new-abap-table-sum/

Sandra_Rossi
Active Contributor
0 Kudos

Please indicate the EXACT error message.

About using COLLECT, the documentation says: "All components that are not part of the primary table key must have a numeric data type."

How did you define your internal table, what is the primary table key, especially what components are NOT part of the primary table key?

hagit
Active Participant
0 Kudos

Did you use ‘with EMPTY KEY’ ?

If yes then you have to replace it – “WITH KEY <name of the non numeric field>”

For example replace line 18 with line 19:

TYPES:
BEGIN OF sty,
ch(3) TYPE c,
int TYPE i.
TYPES:END OF sty .
TYPES:
tty_orig TYPE TABLE OF sty
* ,tty_sum TYPE TABLE OF sty with EMPTY KEY. "line 18
,tty_sum TYPE TABLE OF sty WITH KEY ch . "line 19


DATA: lt_orig TYPE tty_orig
,ls_orig LIKE LINE OF lt_orig
,lt_sum TYPE tty_sum
.

ls_orig-ch = 'a'.
ls_orig-int = 1.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'a'.
ls_orig-int = 3.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'b'.
ls_orig-int = 11.
APPEND ls_orig TO lt_orig.

ls_orig-ch = 'b'.
ls_orig-int = 33.
APPEND ls_orig TO lt_orig.

clear ls_orig.
LOOP AT lt_orig INTO ls_orig .
COLLECT ls_orig INTO lt_sum.
ENDLOOP.

Saswata89
Participant
0 Kudos

Ahana,

Please share the structure of your internal table and more details(screen capture etc.) on the error. You might also take help from this answer - https://answers.sap.com/questions/2880928/use-of-collect-statement.html