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

using COLLECT on a internal table

Former Member
0 Likes
31,464

Hello,

I have fetched records from a table into my internal table through, " SELECT * FROM .... INTO TABLE FOR ALL ENTRIES IN ...... "

There are two columns in my internal table against which i need a total of the column. COLLECT sums the amounts . But how can i use COLLECT in this scenario ?

Can anyone kindly guide me ...Thanks

Shehryar Dahar

Hello,

I have fetched records from a table into my internal table through, " SELECT * FROM .... INTO TABLE FOR ALL ENTRIES IN ...... "

There are two columns in my internal table against which i need a total of the column. COLLECT sums the amounts . But how can i use COLLECT in this scenario ?

Can anyone kindly guide me ...Thanks

Shehryar Dahar

7 REPLIES 7
Read only

Former Member
0 Likes
7,836

Hi shehryar,

Collect statement is used to collect the all values and place into one.

For Example,

Emp.no Salary

1 1000

2 1000

3 1000

1 1000

4 1000.

Here The Emp.no 1 Comes Twice. So it will add the values like,

1 2000

2 1000

3 1000

4 1000.

for your Requirement,

collect IT.

Thanks.

Read only

Former Member
0 Likes
7,836

collect will occur only on the first field in your internal table...

Read only

Former Member
0 Likes
7,836

Hi

Collect command is used to sum up numeric values only when their non numeric values are equal.

Eg: itab contains 2 coloums

f1 f2

a 10

b 10

a 20

c 21

d 23

b 10

Here when u give collect statement it will add numeric values of a and b ony not c and d their non numeric values are not same.

Hope u understood the point

Regards

Haritha.

Read only

Former Member
0 Likes
7,836

Create a temp internal table of the same structure.

Loop at Itab1 into wa.

COLLECT wa INTO itab2.

endloop.

ur itab2 will have the sums..

but if u want the totals of the column..

i would suggest you to go for control break statements.

loop at itab into wa.

at last.

sum.

endat.

endloop.

u will have the sums populated in ur wa

santhosh

Read only

Former Member
0 Likes
7,836

Hi,

COLLECT add the numeric entries for the corresponding table keys..

Example.

-


DATA: BEGIN OF ITAB OCCURS 0,

MATNR TYPE MATNR,

COUNT TYPE I,

END OF ITAB.

ITAB-MATNR = 'ABC'.

ITAB-COUNT = 2.

COLLECT ITAB.

ITAB-MATNR = 'ABC'.

ITAB-COUNT = 1.

COLLECT ITAB.

ITAB-MATNR = 'ABCD'.

ITAB-COUNT = 2.

COLLECT ITAB.

ITAB-MATNR = 'ABCD'.

ITAB-COUNT = 3.

COLLECT ITAB.

LOOP AT ITAB.

WRITE: / ITAB-MATNR, ITAB-COUNT.

ENDLOOP.

For more information on Collect, check this site:

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm

Hope this would help you out.

Regards,

Varun.

Read only

Former Member
0 Likes
7,836

Hi,

Check this Sample code.

TYPES: BEGIN OF t_team,

NAME(20) TYPE C,

score TYPE I,

END OF t_team.

DATA : i_team TYPE STANDARD TABLE OF t_team INITIAL SIZE 0,

wa_team type t_team.

wa_team-NAME = 'Sachin'.

wa_team-score = 10.

COLLECT wa_team INTO i_team.

wa_team-NAME = 'Gangully'.

wa_team-score = 70.

COLLECT wa_team INTO i_team.

wa_team-NAME = 'Sachin'.

wa_team-score = 40.

COLLECT wa_team INTO i_team.

loop at i_team into wa_team.

write: / 'Name :', wa_team-name, 'Scored : ', wa_team-score.

endloop.

Regards,

Suresh

Read only

Former Member
0 Likes
7,836

Got it myself. I guess you all didnt get the question.. thanks a lot though !!!