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

delete duplicate entries with condition

Bharath84
Participant
0 Likes
4,646

Hi All,

Below are the records in my internal table it_glt0.

racct               drcrk             hslvt

0000120000     H     |          853.47-

0000120000     S     |            0.00

0000120001     S     |            0.00

0000120001     H     |            0.00

0000120007     S     |            0.00

0000120007     H     |            0.00

0000120080     S     |            0.00

0000120080     H     |       564258.37-

for each account no, I have debit and credit values. If both the Debit and credit values are zero, then I have to delete that multiple record.

Below should be my final internal table.

racct               drcrk             hslvt

0000120000    H     |          853.47-

0000120000    S     |            0.00

0000120080    S     |            0.00

0000120080    H     |       564258.37-

How to acheive this functionality. Please help.

Thanks,

Haritha

Hi All,

Below are the records in my internal table it_glt0.

racct               drcrk             hslvt

0000120000     H     |          853.47-

0000120000     S     |            0.00

0000120001     S     |            0.00

0000120001     H     |            0.00

0000120007     S     |            0.00

0000120007     H     |            0.00

0000120080     S     |            0.00

0000120080     H     |       564258.37-

for each account no, I have debit and credit values. If both the Debit and credit values are zero, then I have to delete that multiple record.

Below should be my final internal table.

racct               drcrk             hslvt

0000120000    H     |          853.47-

0000120000    S     |            0.00

0000120080    S     |            0.00

0000120080    H     |       564258.37-

How to acheive this functionality. Please help.

Thanks,

Haritha

14 REPLIES 14
Read only

adrian_mejido
Contributor
0 Likes
3,533

Hi Haritha,

You can try this


DELETE ADJACENT DUPLICATES FROM it_glt0 COMPARING racct hslvt



Best Regards.

Read only

0 Likes
3,533

Hi,

I tried this already. I am getting the below.


racct               drcrk             hslvt

|0000120000 |H     |          853.47-

|0000120000 |S     |            0.00

|0000120001 |H     |            0.00

|0000120007 |H     |            0.00

|0000120080 |H     |       564258.37-

|0000120080 |S     |            0.00

Below should be my final internal table.

racct               drcrk             hslvt

0000120000    H     |          853.47-

0000120000    S     |            0.00

0000120080    S     |            0.00

0000120080    H     |       564258.37-

Thanks,

Haritha

Read only

0 Likes
3,533

Hi,

Why you don't try to delete first all the entries that are zero?

The result would be:

0000120000    H     |          853.47-

0000120080    H     |       564258.37-

After that, you would have to add entries (0.0) for each entry that has a value. For that you should check if there is a "H" entry, you would have to insert a "S" entry with 0.0

0000120000    H     |          853.47-

0000120000 S |        0.00

0000120080    H     |       564258.37-

0000120080S |        0.00

Best Regards

Read only

0 Likes
3,533

Hi Haritha,

Please follow the below steps.

Option 1.

Sort your internal table it_glt0.

Loop at it_glto.

use at end of <racct>

sum.

end at.

if sum = 0 ,then append these records into a different temp table which you do not append into your final internal table.

Option 2:

Sort your internal table it_glt0.

LOOP AT IT_GLTO INTO WA_GLTO.

I= SY-TABIX.

C= I + 1,

D = I - 1.

READ TABLE IT_GLTO INTO WA1_GLTO INDEX C.

READ TABLE IT_GLTO INTO WA2_GLTO INDEX D.

now compare racct fields for both WA1_GLTO and WA2_GLTO .

if they are the same and also the debit and credit values for both are zeros ,delete both these records using their index numbers,Esle append the ones with not zeros into the final internal table.

Thanks

Sarita

Read only

0 Likes
3,533

Hi All,

Issue resolved.

Thanks,

Haritha

Read only

Former Member
0 Likes
3,533

hi Haritha,


loop at it_glt0 INTO wa_glt0.

clear : wa_glt0_1

    READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'H'.

      move wa_table1-hslvt to lv_hslvt.

      CLEAR wa_glt0_1.

     READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'S'.

     if wa_table1-hslvt = lv_hslvt.

     delete it_table WHERE racct = wa_table-racct.

     ENDIF.

ENDLOOP.


try this code it will work.


Best regards,


Read only

0 Likes
3,533

Good code Nitin But he needs delete the two lines when the two lines have value 0

Read only

0 Likes
3,533

Hi Nitin,

The above code will work fine but you must have forgotten to add a check for LV_HSLVT and WA_TABLE1-HSLVT equal to zero.

loop at it_glt0 INTO wa_glt0.

clear : wa_glt0_1

    READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'H'.

      move wa_table1-hslvt to lv_hslvt.

      CLEAR wa_glt0_1.

     READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'S'.

    if wa_table1-hslvt  eq 0 and lv_hslvt eq 0.

     delete it_table WHERE racct = wa_table-racct.

     ENDIF.

ENDLOOP.


Now the code will work fine

Read only

ronaldo_aparecido
Contributor
0 Likes
3,533

Hi Haritha.

Please create a program test and past this code  I Did and worked.

*&---------------------------------------------------------------------*

*& Report  ZTESTER

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  ztester.

TYPES:BEGIN OF ty_1,

      racct(10TYPE c,

      drcrk(1)   TYPE c,

      hslvt(6TYPE p DECIMALS 2,

   END OF ty_1.

DATA:t_1 TYPE TABLE OF ty_1,

      t_auxiliary TYPE TABLE OF ty_1,

      w_1 LIKE LINE OF t_1,

       w_auxiliary LIKE LINE OF t_1,

       v_tabix TYPE sy-tabix,

       v_tabix2 TYPE sy-tabix.

w_1-racct = '0000120000'.

w_1-drcrk = 'H'.

w_1-hslvt = '853.47'.

APPEND w_1 TO t_1.

w_1-racct = '0000120000'.

w_1-drcrk = 'S'.

w_1-hslvt = ' 0.00'.

APPEND w_1 TO t_1.

w_1-racct = '0000120001'.

w_1-drcrk = 'H'.

w_1-hslvt = ' 0.00'.

APPEND w_1 TO t_1.

w_1-racct = '0000120001'.

w_1-drcrk = 'S'.

w_1-hslvt = ' 0.00'.

APPEND w_1 TO t_1.

w_1-racct = '0000120007'.

w_1-drcrk = 'S'.

w_1-hslvt = ' 0.00'.

APPEND w_1 TO t_1.

w_1-racct = '0000120007'.

w_1-drcrk = 'H'.

w_1-hslvt = ' 0.00'.

APPEND w_1 TO t_1.

w_1-racct = '0000120080'.

w_1-drcrk = 'H'.

w_1-hslvt = '564258.37'.

APPEND w_1 TO t_1.

w_1-racct = '0000120080'.

w_1-drcrk = 'S'.

w_1-hslvt = '0.00'.

APPEND w_1 TO t_1.

t_auxiliary[] = t_1[].

SORT: t_auxiliary BY racct

                      drcrk,

       t_1 BY racct

              drcrk.

LOOP AT t_1 INTO w_1.

   v_tabix = sy-tabix.

   IF w_1-drcrk = 'H'.

     READ TABLE t_auxiliary INTO  w_auxiliary WITH KEY racct = w_1-racct

                                                       drcrk = 'S' BINARY SEARCH.

     v_tabix2 = sy-tabix.

     IF sy-subrc = 0.

       IF w_1-hslvt = 0 AND w_auxiliary-hslvt = 0.

         DELETE t_1 INDEX v_tabix.

         DELETE t_1 INDEX v_tabix2.

         CONTINUE.

       ENDIF.

       endif.

     ELSEIF w_1-drcrk = 'S'.

       READ TABLE t_auxiliary INTO  w_auxiliary WITH KEY racct = w_1-racct

                                                         drcrk = 'H' BINARY SEARCH.

       v_tabix2 = sy-tabix.

       IF sy-subrc = 0.

         IF w_1-hslvt = 0 AND w_auxiliary-hslvt = 0.

           DELETE t_1 INDEX v_tabix.

           DELETE t_1 INDEX v_tabix2.

           CONTINUE.

         ENDIF.

       ENDIF.

     ENDIF.

ENDLOOP.

check sy-subrc = 0.

Read only

Former Member
0 Likes
3,533

Here is what you can do,

  • Define another table, say tTable with two fields racct, hslvt
  • Loop through your original Table, and use COLLECT to append records in tTable
  • Delete all records from tTable which has hslvt = 0
  • Loop through your original Table and seek field racct in tTable. If Sy-SubRc NE 0 then the record is to be deleted from your original Table.

Hope this helps.

-Amit.

Read only

alasivanand
Explorer
0 Likes
3,533

Hi Haritha,

We can achieve this in couple of steps.

  • Copy the Internal table into a temporary Internal table IT_TEMP.
  • SORT IT_TEMP by RACCT.
  • DELETE ADJACENT DUPLICATES from the temp table IT_TEMP comparing RACCT. By which you will get the Unique Account Numbers.
  • Loop at IT_TEMP into LS_TEMP.
    • READ TABLE IT_GLT0 INTO LS_GLT0_1 WITH KEY RACCT = LS_TEMP-RACT
                        • DRCRK = 'H'.
    • READ TABLE IT_GLT0 INTO LS_GLT0_1 WITH KEY RACCT = LS_TEMP-RACT
      • DRCRK = 'S'.
    • IF LS_GLT0_1-HSLVT NE 0 AND  LS_GLT0_2-HSLVT NE 0.
      • APPEND LS_GLT0_1 TO IT_FINAL.
      • APPEND LS_GLT0_2 TO IT_FINAL.
    • ENDIF.
    • CLEAR: LS_GLT0_1, LS_GLT0_2.
  • ENDLOOP.

Now you will get the required data into your final internal table IT_FINAL.

Please ask me if any future clarifications required.

Read only

Former Member
0 Likes
3,533

Please check whether the below code will be useful for you.

1) Copy it_glt0 to another internal table.

2) Declare a flag lv_flag

3) Sort it_glt0 based on racct and drcrk.

4) Loop the internal table. If at any point there is a value in hslvt, capture it in the flag.

5) Check whether the flag contains a value. If not delete the new internal table for the corresponding racct.

6) Its pre assumed that drcrk always contains a S or H.

DATA lv_flag.
IT_GLT0_COPY = IT_GLT0.
SORT it_glt0 BY RACCT DRCRK.

LOOP AT it_glt0 INTO WA_glt0.
  IF WA_GLT0-HSLVT IS NOT INITIAL.
    LV_FLAG = 'X'.
  ENDIF.
  AT END OF RACCT.
    IF LV_FLAG ne 'X'.
      DELETE it_glt0_copy WHERE racct = WA_glt0-racct.
    ENDIF.
    CLEAR lv_flag.
  ENDAT.
ENDLOOP.

Read only

0 Likes
3,533

Hi ,

try this below code :


loop at it_glt0 INTO wa_glt0.

clear : wa_glt0_1

    READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'H'.

          and move to other internal table...

      CLEAR wa_glt0_1.

     READ TABLE it_glt0 INTO wa_glt0_1 WITH KEY racct = wa_table-racct drcrk = 'S'.

     here put the conditon for both values are equal  to ZERO.

     delete the data from internal table.

     ENDIF.

ENDLOOP.

Read only

rosenberg_eitan
Active Contributor
0 Likes
3,533

Hi,


Two pass solution:


FORM test_04 .

  TYPES: BEGIN OF tp_data_1 .
  TYPES: racct       TYPE racct ,
         shkzg       TYPE shkzg ,
         hslvt       TYPE hslvt .
  TYPES: END OF tp_data_1 .

  DATA:  it_data_1 TYPE TABLE OF tp_data_1 .
  DATA:  st_data_1 LIKE LINE OF it_data_1 .

  st_data_1-racct = '0000120000'.
  st_data_1-shkzg = 'H'.
  st_data_1-hslvt = '853.47'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120000'.
  st_data_1-shkzg = 'S'.
  st_data_1-hslvt = ' 0.00'.
  APPEND st_data_1 TO it_data_1.


  st_data_1-racct = '0000120001'.
  st_data_1-shkzg = 'H'.
  st_data_1-hslvt = ' 0.00'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120001'.
  st_data_1-shkzg = 'S'.
  st_data_1-hslvt = '0.00'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120007'.
  st_data_1-shkzg = 'S'.
  st_data_1-hslvt = '0.00'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120007'.
  st_data_1-shkzg = 'H'.
  st_data_1-hslvt = '0.00'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120080'.
  st_data_1-shkzg = 'H'.
  st_data_1-hslvt = '564258.37'.
  APPEND st_data_1 TO it_data_1.

  st_data_1-racct = '0000120080'.
  st_data_1-shkzg = 'S'.
  st_data_1-hslvt = '0.00'.
  APPEND st_data_1 TO it_data_1.

  TYPES: BEGIN OF tp_sums_1 .
  TYPES: racct       TYPE racct ,
         counter     TYPE integer .
  TYPES: END OF tp_sums_1 .

  DATA: it_sums_1 TYPE TABLE OF tp_sums_1 .
  DATA: st_sums_1 LIKE LINE OF it_sums_1 .

  FIELD-SYMBOLS: <st_data_1> LIKE LINE OF it_data_1 .

  LOOP AT it_data_1 ASSIGNING <st_data_1> .

    st_sums_1-racct = <st_data_1>-racct .

    IF <st_data_1>-hslvt NE 0 .
      st_sums_1-counter = 1 .
    ELSE .
      st_sums_1-counter = 0 .
    ENDIF .

    COLLECT st_sums_1 INTO it_sums_1 .

  ENDLOOP.

  DELETE it_sums_1 WHERE counter EQ 0 .

  SORT it_sums_1 BY racct .

  LOOP AT it_data_1 ASSIGNING <st_data_1> .

    READ TABLE it_sums_1 TRANSPORTING NO FIELDS
    WITH KEY
      racct = <st_data_1>-racct .

    IF sy-subrc NE 0 .
      DELETE it_data_1 .
    ENDIF .

  ENDLOOP .

BREAK-POINT .

ENDFORM .                                                   "test_04