Skip to Content

Picking only one record from duplicate entries in internal table based on conditions

Hello All,

I have a requirement where ,If I have an internal table as shown below.

Header 1 Header 2 Header 3 Header 4 A B 10 C D E 3 F A B 5 C D E 6 F

For duplicate values comparing header 1,2,4 I should select only the one with max value for header3 and delete the other duplicates

i.e the output should be like

Header 1 Header 2 Header 3 Header 4 AB 10 C D E 6 F

I have tried sorting the internal table by header1 ,2,4 and then deleting adjacent duplicates but it wont work .

Any suggestions on how to solve this?? Do I need to use control break statements.

Thanks!!!

Faiz

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

8 Answers

  • Best Answer
    Posted on Jun 12, 2014 at 06:33 AM

    Have you done this?

    SORT itab BY header1 header2 header3 descending header4.

    delete adjacent duplicates from itab comparing header1 header2 header4.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 12, 2014 at 06:30 AM

    Hi Faiz,

    DATA: BEGIN OF gwa_itab,
    a TYPE c LENGTH 4,
    b TYPE c LENGTH 4,
    c TYPE i,
    d TYPE c LENGTH 4,
    END OF gwa_itab,
    gt_itab LIKE STANDARD TABLE OF gwa_itab.

    START-OF-SELECTION.
    gwa_itab-a = 'A'.
    gwa_itab-b = 'B'.
    gwa_itab-c = 9.
    gwa_itab-d = 'D'.
    APPEND gwa_itab TO gt_itab.

    gwa_itab-a = 'AA'.
    gwa_itab-b = 'BB'.
    gwa_itab-c = 6.
    gwa_itab-d = 'DD'.
    APPEND gwa_itab TO gt_itab.

    gwa_itab-a = 'A'.
    gwa_itab-b = 'B'.
    gwa_itab-c = 10.
    gwa_itab-d = 'D'.
    APPEND gwa_itab TO gt_itab.

    gwa_itab-a = 'AA'.
    gwa_itab-b = 'BB'.
    gwa_itab-c = 10.
    gwa_itab-d = 'DD'.
    APPEND gwa_itab TO gt_itab.

    SORT gt_itab BY a b d c DESCENDING.

    DELETE ADJACENT DUPLICATES FROM gt_itab COMPARING a b d.

    LOOP AT gt_itab INTO gwa_itab.
    WRITE:/, gwa_itab-a,gwa_itab-b, gwa_itab-c, gwa_itab-d.
    ENDLOOP.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 12, 2014 at 06:19 AM

    Hello Faiz,

    Sort column 1,2 by asscending and 3 & 4 by descending and delete comparing column 1 2 and 4.

    SORT ITAB BY col1 col2 col3 descending col4 descending.

    Thanks

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 12, 2014 at 06:40 AM

    Hi Rahman,

    As everybody suggested use...SORT but apart from that use addition STABLE.

    SORT itab BY 1 2 3 4 STABLE descending .

    delete adjacent duplicates from itab comparing 1 2 4.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 12, 2014 at 06:42 AM

    Try this,

    SORT itab BY header1 header2 header4 ascending header3 descending.

    delete adjacent duplicates from itab comparing header1 header2 header4.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 12, 2014 at 06:55 AM

    Hi Faizur Rahman,

    Try this,

    SORT itab BY header1 header2 header4 ascending header3 descending.

    DELETE adjacent duplicates from itab comparing header1 header2 header4.

    -- Ritesh

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 12, 2014 at 06:55 AM

    Hi

    Sort the internal table by header1 ,2,4 assending and header 3 descending then delete adjacent duplicates copmaring 1 2 4.

    Example -

    TYPES : BEGIN OF ty_itab,
    h1 TYPE c LENGTH 4,
    h2 TYPE c LENGTH 4,
    h3 TYPE i,
    h4 TYPE c LENGTH 4,
    END OF ty_itab.

    DATA : t_itab TYPE STANDARD TABLE OF ty_itab,
    w_itab TYPE ty_itab.


    CLEAR w_itab.
    w_itab-h1 = 'A'.
    w_itab-h2 = 'B'.
    w_itab-h3 = 10.
    w_itab-h4 = 'C'.
    APPEND w_itab TO t_itab.

    CLEAR w_itab.
    w_itab-h1 = 'D'.
    w_itab-h2 = 'E'.
    w_itab-h3 = 3.
    w_itab-h4 = 'F'.
    APPEND w_itab TO t_itab.
    CLEAR w_itab.

    w_itab-h1 = 'A'.
    w_itab-h2 = 'B'.
    w_itab-h3 = 5.
    w_itab-h4 = 'C'.
    APPEND w_itab TO t_itab.
    CLEAR w_itab.

    w_itab-h1 = 'D'.
    w_itab-h2 = 'E'.
    w_itab-h3 = 6.
    w_itab-h4 = 'F'.
    APPEND w_itab TO t_itab.
    CLEAR w_itab.

    SORT t_itab BY h1 h2 h4 ASCENDING h3 DESCENDING.

    DELETE ADJACENT DUPLICATES FROM t_itab COMPARING h1 h2 h4.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 12, 2014 at 07:18 AM

    Sir,

    Kindly check it.

    types: begin of ty_tab,

    h1(02) type c,

    h2(02) type c,

    h3 type i,

    h4(02) type c,

    end of ty_tab.

    data: IT_TAB TYPE TABLE OF TY_TAB,

    WA_TAB TYPE TY_TAB.

    START-OF-SELECTION.

    WA_TAB-H1 = 'A'. WA_TAB-H2 = 'B'. WA_TAB-H3 = '10'. WA_TAB-H4 = 'C'.

    append wa_tab to it_tab. clear wa_tab.

    WA_TAB-H1 = 'D'. WA_TAB-H2 = 'E'. WA_TAB-H3 = '3'. WA_TAB-H4 = 'F'.

    append wa_tab to it_tab. clear wa_tab.

    WA_TAB-H1 = 'A'. WA_TAB-H2 = 'B'. WA_TAB-H3 = '5'. WA_TAB-H4 = 'C'.

    append wa_tab to it_tab. clear wa_tab.

    WA_TAB-H1 = 'D'. WA_TAB-H2 = 'E'. WA_TAB-H3 = '6'. WA_TAB-H4 = 'F'.

    append wa_tab to it_tab. clear wa_tab.

    sort it_tab by h1 h2 h4 ascending h3 descending.

    delete adjacent duplicates from it_tab comparing h1 h2 h4.

    Regards,

    Venkat

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.