Application Development 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: 

logic needed

Former Member
0 Kudos

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

9 REPLIES 9

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

0 Kudos

I dont want to delete the other records,

i sum the two fields to get the highest total and corresponing field4 is taken

then for th same entry in field one all the field2 and field 3 are added and displayed./

eg

field1 field2 field3 field4

123 50 20 abc

123 60 50 eee

124 70 100 rgg

124 70 90 rrrr

the o/p should be

123 110 70 eee (sum field2&3) field 4 base don the highest total of f3&4

124 140 190 rgg

0 Kudos

hi,

use collect statement. it will sum up all numeric fields if the records are having same fields.

loop at itab.

collect itab.

endloop.

write:/10 itab-fld.....

if helpful reward some points.

with regards,

Suresh Aluri.

Former Member
0 Kudos

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.

0 Kudos

HERE THE OUTPUT IS 'RRR'.

WHAT ABT THE OTHERS

0 Kudos

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.

sushant_singh
Participant
0 Kudos

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.

0 Kudos

if you do not want to delete other record then just omit the delete statement.