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

help on query

Former Member
0 Likes
845

hi guys

I have data in an internal table data like this below

A B C D

1 2 3 15

1 2 3 20

1 2 3 15

2 3 4 10

3 4 5 15

3 4 5 20

i want to compare three fields(A,B & c) if they are same i have to add the d and give a single result like below

A B C D

1 2 3 45

2 3 4 10

3 4 5 35

How can i handle this in coding?

Regards

senthil

1 ACCEPTED SOLUTION
Read only

former_member194152
Contributor
0 Likes
813

hi,

You can use COLLECT statment for this.

But keep in mind using collect statement all non character field will sum up so in ur internal table first 3 field should be of C N and summing field should be of F,I

Regards

Gagan

6 REPLIES 6
Read only

Former Member
0 Likes
813

Hi Senthil,

For your Requirement you can use Collect statement.

COLLECT is used to Summarize the Data in internal table while adding the rows.

Collect <wa> into <itab>.

This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)

If a row is found with the same key:

It will add the Numeric fields instead of creating a new row.

If a row is not found with the same key:

It will create a new row like Append.

DATA : BEGIN OF ITAB1 OCCURS 0,

MATNR TYPE MARD-MATNR,

WERKS TYPE MARD-WERKS,

LABST TYPE MARD-LABST,

END OF ITAB.

DATA :WA LIKE ITAB1.

DATA: ITAB2 LIKE ITAB1 OCCURS 0.

SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.

LOOP AT ITAB1 INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

Check the contents of both ITAB1 AND ITAB2.

Thanks,

Reward If Helpful.

Read only

former_member194152
Contributor
0 Likes
814

hi,

You can use COLLECT statment for this.

But keep in mind using collect statement all non character field will sum up so in ur internal table first 3 field should be of C N and summing field should be of F,I

Regards

Gagan

Read only

hymavathi_oruganti
Active Contributor
0 Likes
813

collect statement should help u.

click f1 on collect statement and know the details about it

Read only

Former Member
0 Likes
813

Hi,

Try like this first sort the data by A,B and C and then use the at end event to do the sum like

sort itab by a b c.

at-end of a.

sum.

endat.

Regards,

Himanshu Verma

Read only

Former Member
0 Likes
813

suppose itab1 contains the value

A B C D

1 2 3 15

1 2 3 20

1 2 3 15

2 3 4 10

3 4 5 15

3 4 5 20

now declare itab2 like itab1(another int table).

loop at itab1.

read table itab2 with key a = itab1-a b = itab1-b c = itab1-c.

if sy-subrc = 0.

itab2-d = itab2-d + itab1-d.

modify itab2 index sy-tabix.

else.

move-corresponding itab1 to itab2.

append itab2.

endif.

endloop.

now itab2 contains your desired value.

note that here itab1 and itab2 with header line( I have assumed) if it is without header line int table then you have to use explicit work area.

regards

shiba dutta

Read only

Former Member
0 Likes
813

SEE YOUR <b>PROBLEM RESOLVED</b> SURELY

DATA : ITAB1 LIKE STANDARD TABLE OF ITAB WITH HEADER LINE.

DATA : ITAB2 LIKE STANDARD TABLE OF ITAB WITH HEADER LINE.

sort itab by A B C.

itab1[] = itab[].

loop at itab.

LOOP AT itab1 WHERE A = ITAB-A AND

B = ITAB-B AND

C = ITAB-C.

IF SY-SUBRC = 0.

ITAB2-D = ITAB2-D + ITAB1-D.

ENDIF.

ENDLOOP.

MOVE ITAB-A TO ITAB2-A.

MOVE ITAB-B TO ITAB2-B.

MOVE ITAB-C TO ITAB2-C.

APPEND ITAB2.

CLEAR ITAB2.

ENDLOOP.

NOW SEE OUTPUT OF ITAB2.

REWARD IF USEFUL..

AMIT SINGLA