Former Member

# help on query

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

10|10000 characters needed characters exceeded

### Related questions

Former Member
Posted on Sep 21, 2007 at 08:22 AM

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

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 21, 2007 at 08:42 AM

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

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 21, 2007 at 08:21 AM

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.

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,

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 21, 2007 at 08:27 AM

collect statement should help u.

click f1 on collect statement and know the details about it

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 21, 2007 at 08:29 AM

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

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 21, 2007 at 08:31 AM

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