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

about collect

Former Member
0 Likes
906

i know collect is used to create unique datasets in internal table. but in the internal, how the abap know which is the key fields. for example,

select matnr shkzg menge from mseg

itab-matnr = mseg-matnr

itab-menge= mseg-menge

collect itab.

endselect.

Do above example group by matnr? If do, how the abap know i want to group by matnr? Thanks!

i know collect is used to create unique datasets in internal table. but in the internal, how the abap know which is the key fields. for example,

select matnr shkzg menge from mseg

itab-matnr = mseg-matnr

itab-menge= mseg-menge

collect itab.

endselect.

Do above example group by matnr? If do, how the abap know i want to group by matnr? Thanks!

5 REPLIES 5
Read only

Former Member
0 Likes
860

Hi,

All the character fields are the key fields(collective key field) in internal table for COLLECT.

So it will add all the quantity, numeric fields based on char/date fields.

Regards,

Atish

Read only

Former Member
0 Likes
860

No, check the details about GROUP BY.

GROUP-BY clause

Variants:

... GROUP BY f1 ... fn

... GROUP BY (itab)

Variant 1

... GROUP BY f1 ... fn

Effect

Groups database table data in a SELECT command on one line in the result set. A group is a set of lines which all have the same values in each column determined by the field descriptors f1 ... fn.

... GROUP BY f1 ... fn always requires a list in the SELECT clause. If you use field descriptors without an aggregate funciton in the SELECTclause, you must list them in the GROUP BY f1 ... fn clause.

Example

Output the number of passengers, the total weight and the average weight of luggage for all Lufthansa flights on 28.02.1995:

TABLES SBOOK.

DATA: COUNT TYPE I, SUM TYPE P DECIMALS 2, AVG TYPE F.

DATA: CONNID LIKE SBOOK-CONNID.

SELECT CONNID COUNT( * ) SUM( LUGGWEIGHT ) AVG( LUGGWEIGHT )

INTO (CONNID, COUNT, SUM, AVG)

FROM SBOOK

WHERE

CARRID = 'LH' AND

FLDATE = '19950228'

GROUP BY CONNID.

WRITE: / CONNID, COUNT, SUM, AVG.

ENDSELECT.

ashish

Read only

Former Member
0 Likes
860

Hi

The COLLECT groups all records with the same key, the key for internal table is all CHAR fields.

So in your case your table has only one char field (MATNR), so it'll be the key.

If your table was:

DATA: BEGIN OF ITAB OCCURS 0,
        ZEILE LIKE MSEG-ZEILE,
        MATNR LIKE MSEG-MATNR,
        MENGE LIKE MSEG-MENGE,
      END OF ITAB.

In this case the key is the fields ZEILE and MATNR

Max

Read only

Former Member
0 Likes
860

If you simply press F1 on COLLECT, you'll see how this statement works.

Rob

Read only

0 Likes
860

Collect works with all no numeric fields as the key. And you must create the internal table specifying those fields as the key.

Alberto