cancel
Showing results for 
Search instead for 
Did you mean: 

regarding Collect...

Former Member
0 Kudos

Hello friends...

Please guide me in this:

I have an internal table with say 6 fields.

The first field is say object_name based on certain conditions.

Now I have 20 record for this internal table and 5 different object names which are repeated somewhere.

Say : these are the fields of my internal table:

(F1) F2 F3 F4 F5 F6

*******************************************************

a

b

a

c

a

b......etc, where f1 is the objective name..

Now after all the data being retrieved I need to Summarize this data by Object name. which sud look like this

a

b

c

So wat sud be the proper syntax of doin this.

Please assist.

Helpful replies will be awarded.

Regards,

Sapna A.

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi ,

<b>COLLECT:</b>

IT IS USED TO A GET A RECORD FROM HEADER TO THE BODY AREA BUT IT WILL NOT ALLOW ANY DUPLICATION EXCEPT IF THERE IS ANY NUMERIC FIELS IT ADDS THAT FIELDS DATA BUT NOT AS A NEW RECORD

<b>Example</b>

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

Regards,

Kumar.

Former Member
0 Kudos

Hi,

Try this:

Declare a work area(x_itab_b) & intenal table(itab_b) of same type in which you have the data. Now loop the table into into the work area & use collect itab_b.

e.g:

loop at itab into x_itab_b.

collect x_itab_b to itab_b.

endloop.

Reward points if helpful.

Ashvender

Former Member
0 Kudos

you can use the syntax / code given by Tamas, please note the collect statement takes the non-numeric field as key and summarize the numeric fields

Former Member
0 Kudos

hi

i think u should go for delete adjacent duplicates.....after sorting the tableby objective name

Former Member
0 Kudos

Hi Sapna,

Proceed like this..

Create another table with same structure as tab1.

LOOP AT tab1.

tab2 = tab1.

COLLECT tab2.

ENDLOOP.

Only the numeric fields will be summed up.

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Former Member
0 Kudos

But the problem is that I am asked to summarise the data by object name.

If there are 6 values for object name , only 6 values shoulb be displayed ,

with all other fields being added up..

regards,

Sapna A.

Former Member
0 Kudos

HI,

if you want to summarize based on the object name .

1) if object name is the only non numeric field and reset of them are numeric then it works fine.. collect will sum up the numeric fields for the all the unique records of non numeric keys...

2) i think this is ur situation.

sort itab by objectname.

loop at itab.

lv_total = lv_total + itab-value.

at end of objectno.

jtab = itab.

jtab-value = lv_total.

append jtab.

clear lv_total

endat.

endloop.

at end of this jtab will have the sum based on the object number.

thaks

Mahesh

Former Member
0 Kudos

Hi!

We can say, itab_destination has 2 fields, object_name and object_value.

SORT itab BY object_name.

REFRESH itab_destination.

LOOP AT itab INTO wa.

CLEAR wa_destination.

MOVE-CORRESPONDING wa TO wa_destination.

COLLECT wa_destination TO itab_destination.

ENDLOOP.

REgards

Tamá