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: 

appropriate use of COLLECT statement

sumit_kumar5
Explorer
0 Kudos

I have one internal table in it we have Account number, and for one account number we have multiple document numbers with different amounts respectively. In the current program only account number and sum of all document number's amount is getting displayed through COLLECT statement but now user wants document number and respective amount in the ALV as well. So I feel now COLLECT statement will not work. Total should be displayed in the new line, suppose 5 document numbers are there then total should be displayed at the 6th line. I tried one test program and fetched data from SFLIGHT table but my total is coming in at the top of the table.

5 REPLIES 5

sumit_kumar5
Explorer
0 Kudos

collect-program.txt

Sample Program attached.

0 Kudos

Could you please provide a code which compiles! (declaring lw_test twice leads to a syntax error)

NTeunckens
Active Contributor

Why not just look into SAP Help Documentation on COLLECT : link

raymond_giuseppi
Active Contributor

If you are using ALV, don't use COLLECT, just fill the internal table at document level, sort by account number, declare amount as sum, define account level for subtotal. (If you add a sum record to the table, it will carry an initial document number so will be sorted by default before the records with a document number)

If you are using CL_SALV_TABLE (hopefully) use:

  • cl_salv_aggregations (table->get_aggregations, methods: opt clean, add_aggregation) to sum a column
  • cl_salv_sorts (table->get_sorts, clean, add_sort) to sort and define sub totals

iftah_peretz
Active Contributor
0 Kudos

I do second Raymond's answer. Nonetheless, I recently came across this. Maybe it can be of use to you.