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: 

Insert a line in middle of internal table for counting total.

hungnth2709
Explorer
1,117

Hi everyone, I'm newbie in abap. I have a question which need for helping.

I have a internal table has 2 column (name and item). I want insert a new column named "TOTAL" at middle and last internal table for sum total same value in name column. Like Picture.

How can i do?

Pls help me. Thanks a lot.

12 REPLIES 12

former_member736527
Active Participant
0 Kudos
801

If this is for an ALV, you need to calculate subtotals. Below link will help

ALV Subtotals

0 Kudos
801

Thanks for your answer. But I want to do it in code for calculate other column.

0 Kudos
801
hungnth2709 If it's ALV, you may also adjust the contents of other columns of the subtotals lines, and let ALV do the subtotals of numeric columns.

0 Kudos
801

Have another method to solve in abap??

0 Kudos
801

I'd do something old school like below.

sort main_table by col1.

read table main_table into work_area index 1.

If sy-subrc IS initial.

old_val = work_area-col1.

Endif.

loop at main_table into work_area.

if work_area-col1 <> old_val.

new_line-col1 = "Total".

new_line-col2 = sum.
insert new_line TO main_table INDEX sy-tabix.
clear sum.
old_value = work_area-col1.
endif.

sum = sum + work_area-col2.
endloop.


*to add the total of the last set of records
new_line-col1 = "Total".

new_line-col2 = sum.
insert new_line TO main_table INDEX sy-tabix.

0 Kudos
801

thanks for your answer.

Sandra_Rossi
Active Contributor
801

Just read the ABAP documentation concerning the statements for working with internal tables.

matt
Active Contributor
0 Kudos
801

I'd define a nested internal table - something like this.

TYPES: BEGIN OF ty_item,
          item TYPE i,
       END OF ty_item.
TYPES ty_items TYPE HASHED TABLE OF ty_item WITH UNIQUE KEY item.
TYPES: BEGIN OF ty_thing,
         name TYPE string,
         total TYPE i,
         items TYPE ty_items,
       END OF ty_thing.
DATA things TYPE HASHED TABLE OF ty_thing WITH UNIQUE KEY name.

But it really depends on what you want to do with the data.

0 Kudos
801

I want to do like picture with abap for calculate profit/loss in CO module.

matt
Active Contributor
801

I'll admit that I don't know the CO module, but an internal table with records that are total lines just seems wrong. I'd either hold the total as name level, or calculate it as I was outputting the report.

This seems like writing a report with output actually being the internal table.

Patrick_vN
Active Contributor
801

First, let me say that it is great you're starting with ABAP!

Second, the solution to your question is not that complicated. Just define the steps you need to do. And then do some research and pick the correct statement.

For instance:

  • Go through the internal table and deal with one record at a time ( = LOOP statement)
  • Check if the name value is the same as the name value in the previous record (= IF statement)
  • if yes = calculate total
  • if no add new record to table with a new name ( ex. CONCATENATE) and the calculated total
  • ...

That way you should be done in no-time. Good luck!

raymond_giuseppi
Active Contributor
0 Kudos
801

I would recommend using the ALV grid to display your data, that without modifying the internal table that you read from the database. ALV will also provide a lot of options for your users. Define a sort criteria on the first field with subtotals, insure the amount field can be summed.

If this is (only) an Abap exercise, you can use loops with statements like LOOP AT and possibly after applying sorting by the SORT command, or even in the definition of the internal table, TYPE HASHED/SORTED TABLE, using the key breaks statements AT NEW/AT END OF.