4

I have an internal table that is filled with 108 entries. From 9 to 9 entries it's repeating the entries and I wanted to delete those duplicates. Since they're exactly the same I used the delete adjacent duplicates from itab comparing all fields. Also tried without comparing all fields. No success.

If it helps, my table has 9 fields: bukrs, hkont, gjahr, belnr, budat, waers, shkzg, wrbtr, dmbtr and dmbe2. They're from BSIS and they're in this order too. This is the DO loop in which is the SELECTenter code here. I've putted the DELETE outside of the DO loop.

The two first SELECT's are working fine and belong to the previous code that existed.

DO 12 TIMES.
         lv_aux = lv_aux + 1.
         lv_tamanho = STRLEN( lv_aux ).
         IF lv_tamanho = 1.
           CONCATENATE '0' lv_aux INTO lv_aux.
         ENDIF.
         CONCATENATE p_gjahr lv_aux '01' INTO z_v_first_day.

         PERFORM get_last_day_of_month USING z_v_first_day
                                       CHANGING lv_last_day.

         " some other code irrelevant to the issue

         SELECT bukrs hkont gjahr belnr budat waers shkzg dmbtr wrbtr dmbe2 FROM bsis
           APPENDING CORRESPONDING FIELDS OF TABLE gt_bancbsis
           WHERE hkont = '0051100001'
           AND bukrs EQ p_bukrs
           AND budat <= lv_last_day.

         " some other code irrelevant to the issue
ENDDO.

DELETE ADJACENT DUPLICATES FROM gt_bancbsis COMPARING ALL FIELDS.

This is a picture of the internal table gt_bancbsis in the dubugger. itab in the debugger

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Eva Dias
  • 1,709
  • 9
  • 36
  • 67
  • 1
    Could you please provide us with both a code sample and the actual data you're seeing? It's hard to tell what's going wrong without further information. – vwegert Nov 06 '12 at 16:50
  • Our machine is down at the moment, so I'm not able to give you much more info right now. As soon the machine is up I'll post the code. – Eva Dias Nov 06 '12 at 16:58
  • I can post the internal table with the data retrieved. – Eva Dias Nov 06 '12 at 17:06
  • This does not help. Please post EXACTLY the data that causes the problem, not some fake data. And if possible please post a screenshot of the ABAP Debugger because a lot of conversion may take place when exporting to excel. – vwegert Nov 06 '12 at 17:16
  • As I said, the system is down. The only thing I have right now is this ans the select that is being executed 12 times. – Eva Dias Nov 06 '12 at 17:24
  • Eva, you're suggesting that a language element used by thousands of developers in tens of thousands of programs around the world is buggy. This isn't impossible, it's just not very likely. Simply poking around with keywords you don't understand is dangerous (and a waste of time as well). I would really like to give you a helpful answer, but you're making it really really hard to help you. – vwegert Nov 07 '12 at 10:47
  • @vwegert I'm not understanding your comment. If I didn't post the code that I have it's because I can't, not because I don't want to. I'm sure you know a lot more than me (that's why I'm asking for help), because I'he only been programming ABAP for about 6 months (which is nothing, I believe). I'm sorry if I don't quite understand sometimes what you try to tell me, but I'm sure you'll understand that explaining things to a newbie it's not always cherry pie. – Eva Dias Nov 07 '12 at 11:10
  • This is not about me knowing more than you, it's about you making it unnecessary hard to answer your questions by providing incomplete or misleading information. Please take a look at http://www.catb.org/~esr/faqs/smart-questions.html and http://www.chiark.greenend.org.uk/~sgtatham/bugs.html to get an idea of what I'm talking about. – vwegert Nov 07 '12 at 11:32

2 Answers2

8

The word ADJACENT in the statement DELETE ADJACENT DUPLICATES is there for a very good reason: It states that only duplicate lines that are next to each other are removed. This is also stated in the online keyword documentation:

Rows are considered to be duplicate if the content of neighboring row is the same in the components examined. In the case of multiple duplicate rows following one another, all the rows (except for the first) are deleted.

This means: If your table contains the values

 A B
 C D
 A B
 C D

it contains duplicate values, but since these are not adjacent, DELETE ADJACENT DUPLICATES will not remove them, no matter what you specify.

The SELECT statement on the other hand does not guarantee a specific order when returning the datasets selected unless you tell it to ORDER BY one or more columns. The rows are just returned in any order, and if the DELETE ADJACENT DUPLICATES statement works, it's pure coincidence. It might even work on one system, stop working on another and remove only half of the duplicates on a third system. So, cardinal rule:

Make sure that your internal table is sorted by the fields you want to be checked for duplicates BEFORE deleting the duplicates.

For better scalability, you should use the SORT statement instead of having the database sort the rows with ORDER BY. So you could use either

SORT gt_bancbsis.
DELETE ADJACENT DUPLICATES FROM gt_bancbsis.

or, if you only want to check for certain fields,

SORT gt_bancbsis BY foo bar baz.
DELETE ADJACENT DUPLICATES FROM gt_bancbsis COMPARING foo bar baz.
vwegert
  • 18,371
  • 3
  • 37
  • 55
  • Thank you. I believed that it would delete all the lines that were equal, except for one... my only problem is only the sorting. Thanks. – Eva Dias Nov 08 '12 at 09:16
1

In BSIS table, you are using 4 jey fields( bukrs, hkont, gjahr, belnr ). Use these field only for sorting.

  1. Sort the internal table first.

    SORT ITAB WITH KEY ITAB-FIELDS.
    
  2. Then COMPARING fields.

     DELETE ADJACENT DUPLICATES FROM ITAB
    

    It will work fine.

Blue
  • 22,608
  • 7
  • 62
  • 92
Dhivya
  • 518
  • 2
  • 8
  • 17
  • Thanks. So, I just need to replace itab with the name of my internal table, right? I'll try it then. Thank you. – Eva Dias Nov 07 '12 at 09:04