2021 Mar 26 8:36 AM
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.
2021 Mar 26 8:50 AM
If this is for an ALV, you need to calculate subtotals. Below link will help
2021 Mar 26 8:54 AM
Thanks for your answer. But I want to do it in code for calculate other column.
2021 Mar 26 8:57 AM
2021 Mar 26 9:00 AM
2021 Mar 26 9:45 AM
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.
2021 Mar 27 4:14 AM
2021 Mar 26 8:56 AM
Just read the ABAP documentation concerning the statements for working with internal tables.
2021 Mar 26 9:19 AM
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.
2021 Mar 26 9:30 AM
I want to do like picture with abap for calculate profit/loss in CO module.
2021 Mar 26 10:32 AM
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.
2021 Mar 26 9:32 AM
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:
That way you should be done in no-time. Good luck!
2021 Mar 26 12:25 PM
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.