‎2007 May 04 8:57 AM
Dear All,
In a program I have an internal table(itab) with duplicate entries. Now i want to remove the duplicate entries from itab and also want to store single records of those duplicate entries in another internal table(itab2). How to achieve it. Eagerly waiting for your reply....
Regards,
Pulokesh
‎2007 May 04 9:14 AM
sort itab1 by all the fields in the internal table
count = 0.
loop at itab1 into wa1
count = count + 1.
At end of the last field name in the internal table .
if count > 1
append wa1 to itab2.
endif.
count = 0.
end at.
endloop.
delete adjacent duplicates from itab1 .
" itab2 contains the duplicate entires
‎2007 May 04 9:00 AM
DATA: BEGIN OF connection,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
distid TYPE spfli-distid,
distance TYPE spfli-distance,
END OF connection.
DATA connection_tab LIKE SORTED TABLE OF connection
WITH NON-UNIQUE KEY cityfrom cityto
distid distance.
SELECT cityfrom cityto distid distance
FROM spfli
INTO TABLE connection_tab.
DELETE ADJACENT DUPLICATES FROM connection_tab.
connection_tab1[] = connection_tab[].
‎2007 May 04 9:03 AM
Hai,
U first loop the internal table1.Inside that assign any key value to a temporary variable.
Then write select query for getting records from that internal table1 using that temporary variable.
If sy-subrc <> 0 means store this value in the internal table2.
Likewise,u do the process inside the Loop.
Regards,
Padmam.
‎2007 May 04 9:04 AM
use delete adjacent duplicates & assign that itab to ur new itab.
‎2007 May 04 9:05 AM
Hi Pulokesh ,
You need to sort the internal table and then use the command
DELETE ADJACENT DUPLICATES <internal table name>.
This will delete all duplicate entries and hence you will be left with distinct records.
Regards
Arun
‎2007 May 04 9:07 AM
Hi Pulokesh,
To delete a single line of any internal table, use the DELETE statement. You can either use the table key to find and delete a single line using its key, delete a set of lines that meet a condition, or find and delete neighboring duplicate entries. If the table has a non-unique key and there are duplicate entries, the first entry is deleted.
Deleting a Line Using the Table Key
To use the table key of table <itab> as a search key, use one of the following statements:
DELETE TABLE <itab> FROM <wa>.
or
DELETE TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n>.
In the first case, <wa> must be a work area compatible with the line type of <itab>. The values of the key fields are taken from the corresponding components of the work area.
In the second case, you have to supply the values of each key field explicitly. If you do not know the name of one of the key fields until runtime, you can specify it as the content of a field <n i > using the form (<n i >) = <f i >. If the data types of <f i > are not compatible with the key fields, the system converts them.
The system searches for the relevant lines as follows:
Standard tables
Linear search, where the runtime is in linear relation to the number of table entries.
Sorted tables
Binary search, where the runtime is in logarithmic relation to the number of table entries.
Hashed tables
The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.
If the system finds a line, it deletes it from the table and sets SY-SUBRC to zero. Otherwise, SY-SUBRC is set to 4. If the table has a non-unique key and the system finds duplicate entries, it deletes the first entry.
Deleting Several Lines Using a Condition
To delete more than one line using a condition, use the following statement:
DELETE <itab> WHERE <cond>.
This processes all of the lines that meet the logical condition <cond>. The logical condition can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression TABLE LINE. The comparison then applies to the entire line. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.
Deleting Adjacent Duplicate Entries
To delete adjacent duplicate entries use the following statement:
DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>
[COMPARING <f1> <f 2> ...
|ALL FIELDS].
The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria:
Without the COMPARING addition, the contents of the key fields of the table must be identical in both lines.
If you use the addition COMPARING <f1> <f 2> ... the contents of the specified fields <f 1 > <f 2 > ... must be identical in both lines. You can also specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.
If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.
You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.
If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.
Examples
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
LINE-COL1 = 1.
DELETE TABLE ITAB: FROM LINE,
WITH TABLE KEY COL1 = 3.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.
The output is:
2 4
4 16
The program fills a hashed table with a list of square numbers. The DELETE statement delete the lines from the table where the key field COL1 has the contents 1 or 3.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
DELETE ITAB WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.
The output is:
1 1
4 16
The program fills a hashed table with a list of square numbers. The DELETE statement deletes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.
DATA OFF TYPE I.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE C,
END OF LINE.
DATA ITAB LIKE STANDARD TABLE OF LINE
WITH NON-UNIQUE KEY COL2.
LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 2. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 3. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 4. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 5. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
OFF = 0. PERFORM LIST.
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS.
OFF = 14. PERFORM LIST.
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING COL1.
OFF = 28. PERFORM LIST.
DELETE ADJACENT DUPLICATES FROM ITAB.
OFF = 42. PERFORM LIST.
FORM LIST.
SKIP TO LINE 3.
LOOP AT ITAB INTO LINE.
WRITE: AT /OFF LINE-COL1, LINE-COL2.
ENDLOOP.
ENDFORM.
The output is:
1 A 1 A 1 A 1 A
1 A 1 B 2 B 2 B
1 B 2 B 3 B 5 A
2 B 3 B 4 B
3 B 4 B 5 A
4 B 5 A
5 A
The example creates and fills a standard table. Here, the first DELETE statement deletes the second line from ITAB because the second line has the same contents as the first line. The second DELETE statement deletes the second line from the remaining table because the contents of the field COL1 is the same as in the first line. The third DELETE statement deletes the third and fourth line from the remaining table because the contents of the default key field COL2 are the same as on the second line. Although the contents of the default key are the same for the first and the fifth line, the fifth line is not deleted because it is not adjacent to the first line.
Reward points if helpful.
Rewards,
Hemant
‎2007 May 04 9:08 AM
Hi,
Try this:
SORT ITAB WITH <key you want>.
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING <fields you sort>
Create another internal table : ITAB2. of same structure as ITAB1.
MOVE-CORRESPONDING ITAB TO ITAB2.
APPEND ITAB2.
See if it helps.
Regards,
Hemant.
‎2007 May 04 9:14 AM
sort itab1 by all the fields in the internal table
count = 0.
loop at itab1 into wa1
count = count + 1.
At end of the last field name in the internal table .
if count > 1
append wa1 to itab2.
endif.
count = 0.
end at.
endloop.
delete adjacent duplicates from itab1 .
" itab2 contains the duplicate entires
‎2007 May 04 9:16 AM
Hi,
As per my uderstanding u have ITAB with duplicate entries and u want to delete those entries(Let say table ITAB1). U also want to store the "Those deleted entries" into another table ITAB2.
Then code as follows
SORT ITAB BY <Key field>.
ITAB1[] = ITAB[].
*ITAB without duplicates
DELETE ADJACENT DUPLICATES FROM ITAB1.
LOOP AT ITAB INTO wa.
READ TABLE ITAB1
TRANSPORTING NO FIELDS
WITH KEY <key field> = wa-<key field>
BINARY SEARCH.
IF sy-subrc <> 0.
*ITAB with Deleted Duplicate entries.
APPEND wa to ITAB2.
ENDIF.
ENDLOOP.
Regards,
Ranjit Thakur.
<b>Please Mark The Helpful Answer.</b>
‎2007 May 04 9:57 AM
Thanks Ranjit Thakur and Sridhar Srirama for your helpful answers.......
‎2007 May 04 10:06 AM
Hi Pulokesh,
nice, quick and not dirty at all:
data:
itab_duplicates like itab[] with unique key table_line,
itab_uniques like itab[] with unique key table_line.
field-symbols:
<itab> like line of itab[].
loop at itab assigning <itab>.
insert <itab> into table itab_uniques.
check sy-subrc <> 0. "This one comes again - duplicate
insert <itab> into table itab_duplicates.
endloop.
Now you have the unique records in table itab_uniques and the duplicates in table itab_duplicates.
itab[] = itab_uniques.
puts them back in original table
itab[] = itab_duplicates.
puts duplicates in original table.
Give it a try!
Regards,
Clemens