COLLECT statement can be used in the internal table whose non key fields are all numeric. It means the statement finds the key (non numeric fields) and if the records are same then the statement will add all other numeric field values. The prerequisite of this statement is that the internal table must have numeric data type fields which are non key fields. COLLECT statement can be applied on any type of internal tables.
Item
|
Quantity (Kg)
|
Price (Rs)
|
Rice
|
2
|
100
|
Wheat
|
1
|
40
|
Rice
|
1
|
60
|
Tea
|
1
|
120
|
Sugar
|
1
|
90
|
Wheat
|
3
|
90
|
If we run the collect statement upon this table then the output will be as following
Item
|
Quantity (Kg)
|
Price (Rs)
|
Rice
|
3
|
160
|
Wheat
|
4
|
130
|
Tea
|
1
|
120
|
Sugar
|
1
|
90
|
REPORT zabap_gui.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next collecting one single row data into the table
COLLECT wtab INTO itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
COLLECT wtab INTO itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
COLLECT wtab INTO itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
COLLECT wtab INTO itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
COLLECT wtab INTO itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
COLLECT wtab INTO itab.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next collecting one single row data into the table
COLLECT wtab INTO itab.
wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90.
COLLECT wtab INTO itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
COLLECT wtab INTO itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
COLLECT wtab INTO itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
COLLECT wtab INTO itab.
wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
COLLECT wtab INTO itab.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
ENDLOOP.
SKIP.
WRITE '=========================================='.