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

it_table operation

Former Member
0 Likes
885

hi all,

i have obtained certain data in an it_table. suppose it is in the following format.

TYPE DETAILS NUMBERGRP

A ABC 123

A ABC 123

B VVV 555

C WNM 675

i want to delete duplicate entries. i am aware of the syntax 'Delete adjacent duplicates'. i dont want to retain even a single duplicate entry.

Basically, in this case i want to delete both entries which are duplicate i.e. the 1's with type 'A'.

Please help me with a the syntax which would help me do that.

Thanks in advance.

Regards,

Kaustubh

8 REPLIES 8
Read only

Former Member
0 Likes
850

hi,

Sort the internal table and delete adjacent duplicates comparing field1, field2......

Read only

Former Member
0 Likes
850

Hi,

For achiving this, you will have to write a loop.

SORT itab BY field1 field2 field3.
LOOP AT itab.
  IF ( lv_field1 = itab-field1 AND lv_field2 = itab-field2 AND lv_field3 = itab-field3 ).

    DELETE itab WHERE field1 = itab-field1 AND field2 = itab-field2 AND field3 = itab-field3.
    CLEAR: lv_field1, lv_field2, lv_field3.

  ELSE.
    CLEAR: lv_field1, lv_field2, lv_field3.
  ENDIF.
  lv_field1 = itab-field1.
  lv_field2 = itab-field2.
  lv_field3 = itab-field3.
ENDLOOP.

Regards

Vishnu Gupta

Edited by: vishnu gupta on May 25, 2009 3:46 PM

Read only

Former Member
0 Likes
850

Hi,

use below statements

delete adjacent duplicates from it_ekpo comparing type

if required for all fields means use

delete adjacent duplicates from it_ekpo comparing all fields

Thanks,

Suma

Read only

rainer_hbenthal
Active Contributor
0 Likes
850

*&---------------------------------------------------------------------*
*& Report yet_another_report.
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zca_sw10.

TYPES:
  BEGIN OF t_tab,
  typ TYPE c LENGTH 1,
  details TYPE c LENGTH 3,
  num TYPE c LENGTH 3,
  END OF t_tab,

  BEGIN OF t_count,
  typ TYPE c LENGTH 1,
  count TYPE i,
  END OF t_count.

DATA:
  it_count TYPE STANDARD TABLE OF t_count,
  wa_count TYPE t_count,

  it_tab TYPE STANDARD TABLE OF t_tab,
  wa_tab TYPE t_tab.

FIELD-SYMBOLS:
  <pt> TYPE t_tab,
  <pc> TYPE t_count.

START-OF-SELECTION.

  wa_tab-typ = 'A'.
  wa_tab-details = 'ABC'.
  wa_tab-num = '123'.
  APPEND wa_tab TO it_tab.

  wa_tab-typ = 'A'.
  wa_tab-details = 'ABC'.
  wa_tab-num = '123'.
  APPEND wa_tab TO it_tab.

  wa_tab-typ = 'B'.
  wa_tab-details = 'VVV'.
  wa_tab-num = '555'.
  APPEND wa_tab TO it_tab.

  wa_tab-typ = 'C'.
  wa_tab-details = 'WWW'.
  wa_tab-num = '675'.
  APPEND wa_tab TO it_tab.

  WRITE:/ 'Orig. table'.
  LOOP AT it_tab ASSIGNING <pt>.
    WRITE:/ <pt>-typ,  <pt>-details,  <pt>-num.
  ENDLOOP.

  LOOP AT it_tab ASSIGNING <pt>.
    wa_count-typ = <pt>-typ.
    wa_count-count = 1.
    COLLECT wa_count INTO it_count.
  ENDLOOP.

  LOOP AT it_count ASSIGNING <pc> WHERE count > 1.
    DELETE it_tab WHERE typ = <pc>-typ.
  ENDLOOP.

  WRITE:/ 'Compacted. table'.
  LOOP AT it_tab ASSIGNING <pt>.
    WRITE:/ <pt>-typ,  <pt>-details,  <pt>-num.
  ENDLOOP.
Read only

Former Member
0 Likes
850

deleted duplicat entries comparing all fields

Read only

former_member188829
Active Contributor
0 Likes
850

Hi,

DATA:BEGIN OF itab OCCURS 0,
     field1(1) TYPE c,
     field2(3) TYPE c,
     field3    TYPE i,
     END OF itab,

     BEGIN OF itab1 OCCURS 0,
     field1(1) TYPE c,
     field2(3) TYPE c,
     field3    TYPE i,
     END OF itab1.

DATA:count TYPE i,
     itab2 LIKE LINE OF itab.

START-OF-SELECTION.
  itab-field1 = 'A'.
  itab-field2 = 'ABC'.
  itab-field3 = '123'.
  APPEND itab.
  CLEAR itab.

  itab-field1 = 'A'.
  itab-field2 = 'ABC'.
  itab-field3 = '123'.
  APPEND itab.
  CLEAR itab.

  itab-field1 = 'B'.
  itab-field2 = 'VVV'.
  itab-field3 = '555'.
  APPEND itab.
  CLEAR itab.

  itab-field1 = 'C'.
  itab-field2 = 'WNM'.
  itab-field3 = '675'.
  APPEND itab.
  CLEAR itab.
  SORT  itab BY field1.

  LOOP AT itab.
    count = count + 1.
    AT END OF field1.
      IF count > 1.
        READ TABLE itab INTO itab2 INDEX sy-tabix.
        IF sy-subrc EQ 0.
        MOVE itab2 TO itab1.
        APPEND itab1.
        ENDIF.
      ENDIF.
     CLEAR count.
    ENDAT.
*  CLEAR count.
  ENDLOOP.

  LOOP AT itab.
    READ TABLE itab1 WITH KEY field1 = itab-field1.
    IF sy-subrc EQ 0.
      DELETE itab.
      MODIFY itab.
    ENDIF.
  ENDLOOP.

  LOOP AT itab.
    WRITE:/ itab-field1, itab-field2,itab-field3.
  ENDLOOP.

Read only

Former Member
0 Likes
850

Hi,

use the code given below this will solve your issue for sure...

DATA : w_index TYPE sy-tabix.
DATA w_count TYPE i.
LOOP AT it_table.
  w_index = sy-tabix.
  CLEAR w_count.
  LOOP AT it_table WHERE type = it_table-type AND details = it_table-details 
                                   AND numbergrp = it_table-numbergrp.
    ADD 1 TO w_count.
    IF w_count > 1.
      DELETE it_table WHERE type = it_table-type AND details = it_table-details 
                                    AND numbergrp = it_table-numbergrp.
      EXIT.
    ENDIF.
  ENDLOOP.
ENDLOOP.

Regards,

Siddarth

Read only

Former Member
0 Likes
850

Hi kaustubh,

You can try out this syntax:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> [COMPARING... ].

Deletes adjacent duplicate entries, either by comparing the key fields or the comparison fields specified explicitly in the COMPARING addition.