2007 Oct 17 5:00 PM
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!
2007 Oct 17 5:02 PM
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
2007 Oct 17 5:03 PM
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
2007 Oct 17 5:04 PM
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
2007 Oct 17 5:07 PM
If you simply press F1 on COLLECT, you'll see how this statement works.
Rob
2007 Oct 17 5:14 PM
Collect works with all no numeric fields as the key. And you must create the internal table specifying those fields as the key.
Alberto
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |