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: 

Internal Table Group By

Former Member
0 Kudos

Hi everyone,

I got a FM with a table parameter and i want to perform some "group by" operation(like sql group by) on it. Any suggestion??

Regards,

Kit

5 REPLIES 5

Former Member
0 Kudos

Hi Kit,

Can u paste the code please...

regards,

Sai

varma_narayana
Active Contributor
0 Kudos

Hi

To perform Group by Operation on INTERNAL TABLE we can use the CONTROL BREAK events:

SORT ITAB BY KEY.

LOOP AT ITAB.

AT NEW KEY.

<<begin of control level>>

ENDAT.

AT END OF KEY.

<<END of control level>>

ENDAT.

ENDLOOP.

<b>reward if Helpful.</b>

0 Kudos

Hi varma,

Thanks for your reply. Could you give me an example with more than one keys??

Regards,

Kit

0 Kudos

COLLECT

Syntax

COLLECT wa INTO itab [result].

Effect

This statement inserts the contents of a work area wa either as single row into an internal table itab or <b>adds the values of its numeric components to the corresponding values of existing rows with the same key</b>. As of Release 6.10, you can use result to set a reference to the inserted or changed row in the form of a field symbol or data reference.

<b>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).</b>

In standard tables that are only filled using COLLECT, the entry is determined by a temporarily created hash administration. The workload is independent of the number of entries in the table. The hash administration is temporary and is generally invalidated when the table is accessed for changing. If further COLLECT statements are entered after an invalidation, a linear search of all table rows is performed. The workload for this search increases in a linear fashion in relation to the number of entries.

In sorted tables, the entry is determined using a binary search. The workload has a logarithmic relationship to the number of entries in the table.

In hashed tables, the entry is determined using the hash administration of the table and is always independent of the number of table entries.

If no line is found with an identical key, a row is inserted as described below, and filled with the content of wa:

In standard tables the line is appended.

In sorted tables, the new line is inserted in the sort sequence of the internal table according to its key values, and the table index of subsequent rows is increased by 1.

In hashed tables, the new row is inserted into the internal table by the hash administration, according to its key values.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

COLLECT should only be used if you want to create an internal table that is genuinely unique or compressed. In this case, COLLECT can greatly benefit performance. If uniqueness or compression are not required, or the uniqueness is guaranteed for other reasons, the INSERT statement should be used instead.

The use of COLLECT for standard tables is obsolete. COLLECT should primarily be used for hashed tables, as these have a unique table key and a stable hash administration.

If a standard table is filled using COLLECT, it should not be edited using any other statement with the exception of MODIFY. If the latter is used with the addition TRANSPORTING, you must ensure that no key fields are changed. This is the only way to guarantee that the table entries are always unique and compressed, and that the COLLECT statement functions correctly and benefits performance. The function module ABL_TABLE_HASH_STATE can be used to check whether a standard table is suitable for editing using COLLECT.

Example

Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc

FROM sflight

INTO seats.

COLLECT seats INTO seats_tab.

ENDSELECT.

Exceptions

Catchable Exceptions

CX_SY_ARITHMETIC_OVERFLOW

Cause: Overflow in integer field during totals formation

Runtime Error: COLLECT_OVERFLOW

Cause: Overflow in type p field during totals formation

Runtime Error: COLLECT_OVERFLOW_TYPE_P

Non-Catchable Exceptions

Cause: COLLECT used for non-numeric fields

Runtime Error: TABLE_COLLECT_CHAR_IN_FUNCTION

0 Kudos

Hi Check this Example:

data : ITAB TYPE TABLE OF MARD WITH HEADER LINE.

SORT ITAB BY MATNR WERKS .

LOOP AT ITAB.

AT NEW MATNR.

write:/ itab-matnr.

ENDAT.

AT NEW WERKS.

write:/ itab-werks. "Plant

ENDAT.

WRITE:/ ITAB-LGORT, "Storage Location

ITAB-LABST. "Stock

AT END OF WERKS.

SUM. "Sums the numeric field LABST

write:/ 'Sub total for Plant = ' , itab-labst.

ENDAT.

AT END OF MATNR.

SUM. "Sums the numeric field LABST

write:/ 'Sub total for Material = ' , itab-labst.

ENDAT.

ENDLOOP.

<b>REWARD IF HELPFUL.</b>