Skip to Content
author's profile photo Former Member
Former Member

logic needed

Hi All,

i have an internal table with the following values .

eg

field1 field2 field3 field4

123 50 20 abc

123 60 50 eee

123 70 90 rrrr

124 50 80 ggg

124 90 20 ggh

125 50 80 nnn

125 30 20 mm

in the above table

i need to display field4 for the below condition.

for same entry in field1 i should sum field2&field3 and get the highest value.

display field4 which has the highest value.(sum of field2&3)

thanks in advance

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Sep 03, 2007 at 09:34 AM

    data: begin of tab1 occurs 0,

    field1(3),

    field2 type i,

    field3 type i,

    field4(3),

    end of tab1.

    data: begin of tab2 occurs 0,

    field1(3),

    field5 type i,

    field4(3),

    end of tab2.

    loop at tab1.

    tab2-field1 = tab1-field1.

    tab2-field5 = tab1-field2 + tab1-field3.

    tab2-field4 = tab1-field4.

    append tab2.

    endloop.

    sort tab2 by field1 ascending and field5 descending.

    delete adjacent-duplicates from itab2 comparing field1.

    the result is what u requires.

    hope this helps,

    reward if useful.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 03, 2007 at 09:37 AM

    Hi,

    Code like this:

    Add a new field field5 of type field2/3 in the same table.

    LOOP AT tab1.

    field5 = field2 + field3.

    MODIFY tab1.

    ENDLOOP.

    field1 field2 field3 field4 field5

    123 50 20 abc 70

    123 60 50 eee 110

    123 70 90 rrrr 160

    SORT tab1 BY field1 field5.

    field1 field2 field3 field4 field5

    123 70 90 rrrr 160

    123 60 50 eee 110

    123 50 20 abc 70

    DELETE ADJACENT DUPLICATES COMPARING field1.

    field1 field2 field3 field4 field5

    123 70 90 rrrr 160

    Thanks and Best Regards,

    Vikas Bittera.

    ou will get something like

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 03, 2007 at 09:48 AM

    Hi Balaji,

    Use the below code.

    DATA: BEGIN OF ITAB OCCURS 0,

    FIELD1 TYPE I,

    FIELD2 TYPE I,

    FIELD3 TYPE I,

    FIELD4(4) TYPE C,

    END OF ITAB.

    DATA: V_COUNT TYPE I,

    V_COUNT1 TYPE I,

    V_TEXT(4) TYPE C.

    ITAB-FIELD1 = 123.

    ITAB-FIELD2 = 50.

    ITAB-FIELD3 = 20.

    ITAB-FIELD4 = 'abc'.

    APPEND ITAB.

    ITAB-FIELD1 = 123.

    ITAB-FIELD2 = 60.

    ITAB-FIELD3 = 50.

    ITAB-FIELD4 = 'eee'.

    APPEND ITAB.

    ITAB-FIELD1 = 123.

    ITAB-FIELD2 = 70.

    ITAB-FIELD3 = 90.

    ITAB-FIELD4 = 'rrrr'.

    APPEND ITAB.

    ITAB-FIELD1 = 124.

    ITAB-FIELD2 = 50.

    ITAB-FIELD3 = 80.

    ITAB-FIELD4 = 'ggg'.

    APPEND ITAB.

    ITAB-FIELD1 = 124.

    ITAB-FIELD2 = 90.

    ITAB-FIELD3 = 20.

    ITAB-FIELD4 = 'ggh'.

    APPEND ITAB.

    ITAB-FIELD1 = 124.

    ITAB-FIELD2 = 50.

    ITAB-FIELD3 = 80.

    ITAB-FIELD4 = 'nnn'.

    APPEND ITAB.

    ITAB-FIELD1 = 124.

    ITAB-FIELD2 = 30.

    ITAB-FIELD3 = 20.

    ITAB-FIELD4 = 'mm'.

    APPEND ITAB.

    LOOP AT ITAB.

    V_COUNT = ITAB-FIELD2 + ITAB-FIELD3.

    IF V_COUNT > V_COUNT1.

    V_COUNT1 = V_COUNT.

    V_TEXT = ITAB-FIELD4.

    ENDIF.

    ENDLOOP.

    WRITE:/ V_TEXT.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi,

      Code like this:

      Add a new field field5 of type field2/3 in the same table.

      LOOP AT tab1.

      field5 = field2 + field3.

      MODIFY tab1.

      ENDLOOP.

      field1 field2 field3 field4 field5

      123 50 20 abc 70

      123 60 50 eee 110

      123 70 90 rrrr 160

      SORT tab1 BY field1 field5.

      field1 field2 field3 field4 field5

      123 70 90 rrrr 160

      123 60 50 eee 110

      123 50 20 abc 70

      Create another table with only field1, 2 and 3..

      LOOP AT tab1.

      tab2-field1 = tab1-field1.

      tab2-field2 = tab1-field2.

      tab2-field3 = tab1-field3.

      COLLECT tab2.

      ENDLOOP.

      Tab2

      field1 field2 field3

      123 180 160

      Now,

      DELETE ADJACENT DUPLICATES FROM tab1 COMPARING field1.

      LOOP AT tab1.

      READ TABLE tab2 WITH key field1 = tab1-field1.

      If sy-subrc EQ 0.

      tab1-field1 = tab2-field1.

      tab1-field2 = tab2-field2.

      tab1-field3 = tab2-field3.

      MODIFY tab1.

      ENDLOOP.

      Thanks and Best Regards,

      Vikas Bittera.

  • Posted on Sep 03, 2007 at 09:58 AM

    Hi,

    Here is the required logic. just you need to check the syntax in delete and need to define some local variables used below.

    loop at itab.

    loop at itab where itab-field1 = field1.

    index = sy-tabix.

    itab-field4 = itab-field2 + itab-field3.

    modify itab index index transporting field4.

    if itab-field4 > field.

    field = itab-field4.

    endif.

    endloop.

    delete table itab where field4 <> field and field1 = itab-field1.

    endloop.

    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.