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: 
Read only

Summarizing internal tables

Former Member
0 Likes
987

I am extracting data into internal table which 6 fields of which 4 are char type and 2 are curr type.

My concern is if all the 4 char type fields match then i want the curr fileds of the all records to summed and displayed as single entry.

eg:    itab-a    itab-b   itab-c    itab-d      itab-e     itab-f
        123        345     ab          cd          2300     3400
        123        345     ab          cd          4000     5000

Now instead of those two entries i want a single entry to be displayed in the report output.

like


       123        345     ab          cd          6300     8400

How can i achieve that...

5 REPLIES 5
Read only

Former Member
0 Likes
827

Hi Alexander,

try this

Loop at itab.
 COLLECT ITAB.
ENDLOOP.

Read only

Former Member
0 Likes
827

Use AT NEW itab-d i.e. the 4th field..

then use COLLECT

Reward points if useful

Regards

Prax

Read only

Former Member
0 Likes
827

Hi Alex,

You can use Collect statement for that.

loop at <internal table>.

collect <internal table>.

endloop.

<b>

Reward points if this helps,</b>

Kiran

Read only

Former Member
0 Likes
827

Hi,

You can use COLLECT, but the prerequisite for the use of this statement is that wa is compatible with the row type of itab and all components that are not part of the table key must have a numeric data type (i, p, f). If possible change your internal table and use collect which should solve your issue.

Giri..

Read only

Former Member
0 Likes
827

Hi,

The following statement allows you to summate entries in an internal table:

COLLECT <wa> INTO <itab>.

<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type.

When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You should only use the COLLECT statement if you want to create summarized tables. If you use other statements to insert table entries, you may end up with duplicate entries.

Lines are added to internal tables as follows:

Standard tables If the COLLECT statement is the first statement to fill the standard table, the system creates a temporary hash administration that identifies existing entries in the table. The hash administration is retained until another statement changes the contents of key fields or changes the sequence of the lines in the internal table. After this, the system finds existing entries using a linear search. The runtime for this operation increases in linear relation to the number of existing table entries. The system field SY-TABIX contains the

index of the line inserted or modified in the COLLECT statement.

Sorted tables The system uses a binary search to locate existing lines. The runtime for the operation increases logarithmically with the number of existing lines. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.

Hashed tables The system finds existing lines using the hash algorithm of the internal table. After the COLLECT statement, the system field SY-TABIX has the value 0, since hashed tables have no linear index.

Example

DATA: BEGIN OF LINE,

COL1(3) TYPE C,

COL2(2) TYPE N,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE

WITH NON-UNIQUE KEY COL1 COL2.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.

COLLECT LINE INTO ITAB.

WRITE / SY-TABIX.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDLOOP.

The output is:

1

2

1

abc 12 10

def 34 5

The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECT statement, the first line of ITAB is modified.

REgards,

Bhaskar