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

Deletion from Internal table.

Former Member
0 Likes
1,223

Hi Gurus....

My requirement as follows,

In internal table ITAB_1 entries are

aaa 200 1.1 abc

aaa 100 1.1 abc

bbb 200 1.2 bcd

ccc 50 1.1 acd

ccc 25 1.1 acd

ccc 150 1.1 acd

now i need to process this internal table and show the result as

aaa 300 1.1 abc

bbb 200 1.2 bcd

ccc 225 1.1 acd

.......Thanks in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,196

HI,

" Assumption Field3 is of char type

Field1 Field2 Field3 Field4

aaa 200 1.1 abc

aaa 100 1.1 abc

bbb 200 1.2 bcd

ccc 50 1.1 acd

ccc 25 1.1 acd

ccc 150 1.1 acd

Loop at itab_1.
Collect itab_1 to itab_2.
Endloop.

If field 3 of F/P type then declate the itab_2 as same as itab_1 chage the datatype of field3 to char

code}Loop at itab_1.

MOVE:

itab2-field1 to itab1-field1,

itab2-field2 to itab1-field2,

itab2-field3 to itab1-field3,

itab2-field4 to itab1-field4.

Collect itab_1 to itab_2.

clear itab_2.

Endloop.

10 REPLIES 10
Read only

Former Member
0 Likes
1,196

Hi,

loop over the table and use the statement collect.

Save it into a new internal table (or do the action while filling the first internal table)

Afterwards delete adjacent duplicates from table (first sort the table)

Example:

Loop

Collect ... into ...

Endloop

sort xxx by Y

delete adjacent duplicates from xxx comparing Y

Regards,

Peter

Read only

former_member226519
Active Contributor
0 Likes
1,196

define a second ITAB with identical structure and don't APPEND but COLLECT the lines to it.

The amount field has to be defined numeric, the other fields as CHAR.

Read only

Former Member
0 Likes
1,197

HI,

" Assumption Field3 is of char type

Field1 Field2 Field3 Field4

aaa 200 1.1 abc

aaa 100 1.1 abc

bbb 200 1.2 bcd

ccc 50 1.1 acd

ccc 25 1.1 acd

ccc 150 1.1 acd

Loop at itab_1.
Collect itab_1 to itab_2.
Endloop.

If field 3 of F/P type then declate the itab_2 as same as itab_1 chage the datatype of field3 to char

code}Loop at itab_1.

MOVE:

itab2-field1 to itab1-field1,

itab2-field2 to itab1-field2,

itab2-field3 to itab1-field3,

itab2-field4 to itab1-field4.

Collect itab_1 to itab_2.

clear itab_2.

Endloop.

Read only

0 Likes
1,196
...    loop at itab1 into wa1.
     itab2[] = itab1[].
     collect itab2.
     endloop.

This is not working.Whats the mistake.

Read only

0 Likes
1,196

Hi

IF you want to use COLLECT statament:

loop at itab1 into wa1.
*     itab2[] = itab1[].            "<----------Error: you're moving all data of itab1 to itab2
*     collect itab2.                 "<----------Error: Any value is transfered to work area of itab2, so u're collecting an
                                          "                     initial record
     collect wa1 into itab2.
 endloop.

Max

Read only

0 Likes
1,196

HI,

loop at itab1 into wa1.
     collect wa1 INTO itab2.
     endloop.

Read only

0 Likes
1,196

Thanks ...Got it now....

Read only

Former Member
0 Likes
1,196

Hi Deepan,

There are two options. if the 3rd column 1.1 is of character type then you can use COLELCT.


tab1 and tab2 with same structure.

char   numeric(int)   char   char
aaa 200 1.1 abc
aaa 100 1.1 abc
bbb 200 1.2 bcd 
ccc 50 1.1 acd
ccc 25 1.1 acd
ccc 150 1.1 acd


loop at tab1.
  tab2 = tab1.
 collect tab2.
endloop.


otherwise

loop at tab1 into wa_tab1.
wa_tab2-column1 = wa_tab1-column1.
wa_tab2-column2 = wa_tab1-column2 + wa_tab2-column2.
wa_tab2-column3 = wa_tab1-column3.
wa_tab2-column4 = wa_tab1-column4.

on change of column2.
  append wa_tab2 to tab2.
clear wa_tab2.
endon.

endloop.

Like this you can acheive.

Thanks,

Jyothi

Read only

Former Member
0 Likes
1,196

Hi

DATA: BEGIN OF ITAB OCCURS 0,
       FIELD1(3),
       FIELD2(3),
       FIELD3(3),
       VAL   TYPE I,
      END   OF ITAB.


ITAB-FIELD1 = 'aaa'.
ITAB-FIELD2 = '1.1'.
ITAB-FIELD3 = 'abc'.
ITAB-VAL    = 200.
APPEND ITAB.


ITAB-FIELD1 = 'aaa'.
ITAB-FIELD2 = '1.1'.
ITAB-FIELD3 = 'abc'.
ITAB-VAL    = 100.
APPEND ITAB.

ITAB-FIELD1 = 'bbb'.
ITAB-FIELD2 = '1.2'.
ITAB-FIELD3 = ' bcd'.
ITAB-VAL    = 200.
APPEND ITAB.

ITAB-FIELD1 = 'ccc'.
ITAB-FIELD2 = '1.1'.
ITAB-FIELD3 = 'acd'.
ITAB-VAL    = 50.
APPEND ITAB.

ITAB-FIELD1 = 'ccc'.
ITAB-FIELD2 = '1.1'.
ITAB-FIELD3 = 'acd'.
ITAB-VAL    = 25.
APPEND ITAB.

ITAB-FIELD1 = 'ccc'.
ITAB-FIELD2 = '1.1'.
ITAB-FIELD3 = 'acd'.
ITAB-VAL    = 150.
APPEND ITAB.

SORT ITAB.

LOOP AT ITAB.
  AT END OF FIELD3.
    SUM.
    WRITE: / ITAB-FIELD1,
             ITAB-FIELD2,
             ITAB-FIELD3,
             ITAB-VAL.
  ENDAT.
ENDLOOP.

Max

Read only

Former Member
0 Likes
1,196
Sort itab by Fld1 fld3 fld4.

Loop at Itab.
Itab1-fld2 = itab-fld2 + itab1-fld2.
Itab1-fld1 = itab-fld1.
Itab1-fld3 = itab-fld3.
Itab1-fld4 = itab-fld4.

At new fld1.
append itab1.
clear itab1.
endat.

Endloop.

Or Use Collect.

Loop at itab
collect itab to itab1.
endloop.

Note: Fld3 should be char type key field of table

Result:

aaa 300 1.1 abc
bbb 200 1.2 bcd
ccc 225 1.1 acd

Regards,

Gurpreet