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

Internal table issue

Former Member
0 Likes
1,316

Hi friends,

I have list of fields in internal table . in that i have to compare last coulm values of I and S fields combination , finally i have to get only one row for that I and S combination with highest value in the last column . while comparing , if values are equal , then compare next column value and and find which one is lowest .

for eg :

I1 S1 M1 MS1 70 5

I1 S1 M1 MS2 20 6

I1 S2 M2 MS1 10 2

I2 S2 M3 MS2 20 4

I3 S3 M3 MS3 10 8

I3 S3 M4 MS4 50 2

I3 S3 M5 MS5 50 3

Out put is

I1 S1 M1 MS1 70 5

I1 S2 M2 MS1 10 2

I2 S2 M3 MS2 20 4

I3 S3 M4 MS4 50 2

pls let me know if u find idea ..

Regards,

kani.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,278

Hi,

Here is the code which will help you.

DATA : begin of it_in OCCURS 0,

field1(2) TYPE c,

field2(2) TYPE c,

field3(2) TYPE c,

field4(3) TYPE c,

field5 TYPE i,

field6 TYPE i,

END OF it_in.

DATA wa_in LIKE LINE OF it_in.

DATA wa_in1 LIKE LINE OF it_in.

DATA : begin of it_out OCCURS 0,

field1(2) TYPE c,

field2(2) TYPE c,

field3(2) TYPE c,

field4(3) TYPE c,

field5 TYPE i,

field6 TYPE i,

END OF it_out.

DATA wa_out LIKE LINE OF it_out.

wa_in-field1 = 'l1'.

wa_in-field2 = 's1'.

wa_in-field3 = 'm1'.

wa_in-field4 = 'MS1'.

wa_in-field5 = '70'.

wa_in-field6 = '5'.

APPEND wa_in to it_in.

wa_in-field1 = 'l1'.

wa_in-field2 = 's1'.

wa_in-field3 = 'm1'.

wa_in-field4 = 'MS2'.

wa_in-field5 = '20'.

wa_in-field6 = '6'.

APPEND wa_in to it_in.

wa_in-field1 = 'l1'.

wa_in-field2 = 's2'.

wa_in-field3 = 'm2'.

wa_in-field4 = 'MS1'.

wa_in-field5 = '10'.

wa_in-field6 = '2'.

APPEND wa_in to it_in.

wa_in-field1 = 'l2'.

wa_in-field2 = 's2'.

wa_in-field3 = 'm3'.

wa_in-field4 = 'MS2'.

wa_in-field5 = '20'.

wa_in-field6 = '4'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm3'.

wa_in-field4 = 'MS3'.

wa_in-field5 = '10'.

wa_in-field6 = '8'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm4'.

wa_in-field4 = 'MS4'.

wa_in-field5 = '50'.

wa_in-field6 = '2'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm5'.

wa_in-field4 = 'MS5'.

wa_in-field5 = '50'.

wa_in-field6 = '3'.

APPEND wa_in to it_in.

SORT it_in by field1 field2 field5.

LOOP AT it_in INTO wa_in.

IF sy-tabix eq 1.

MOVE wa_in to wa_in1.

else.

IF wa_in-field1 eq wa_in1-field1 AND wa_in-field2 eq wa_in1-field2.

IF wa_in-field5 eq wa_in1-field5.

IF wa_in1-field6 le wa_in-field6.

APPEND wa_in1 to it_out.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ELSE.

APPEND wa_in to it_out.

CLEAR wa_in.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ENDIF.

else.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

CONTINUE.

ENDIF.

ELSE.

APPEND wa_in1 to it_out.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ENDIF.

ENDIF.

ENDLOOP.

10 REPLIES 10
Read only

Former Member
0 Likes
1,278

sorry highest value not in the last column just previous of last column . if that column values are equal , then go for last column and get lowest .

Read only

Former Member
0 Likes
1,278

Can you please explain your question more efficiently. I have an idea but iam not sure it helps you are not.

Explain the question once again i will check with my logic.

Read only

0 Likes
1,278

Hi Lekha,

I have values in internal table . now i have to compare column 5 for I and S combination . and choose highest value if they are all different and get the output with single record with that I and S combination. if you have same highest value with 2 rows , then compare next column value . if u find lowest value in any one row , then get that as output .

pls see example i have given

Thanks and Regards,

kani.

Read only

0 Likes
1,278

Hi Kani,

This works for your requirement. Revert back if any doubts...


DATA : BEGIN OF ITAB OCCURS 0,
  I(2), S(2), M(2), MS(2), F1(2), F2(2), 
  END OF ITAB.
DATA :  I1(2), S1(2), F_1(2).

LOOP AT ITAB .
  IF ITAB-I EQ I1 AND ITAB-S EQ S1.

    IF F_1 GE ITAB-F1.
      DELETE ITAB .
    ELSE.
      DELETE ITAB WHERE I = I1 AND S = S1 AND F1 = F_1.
    ENDIF.

  ENDIF.
  CLEAR : I1, S1, F_1.
  MOVE : ITAB-I TO I1 , ITAB-S TO S1, ITAB-F1 TO F_1.
  
  CLEAR : ITAB.
ENDLOOP.

Thanks & regards,

Dileep .C

Read only

Former Member
0 Likes
1,278

Hi,

Loop at your internal table and for each iteration store the value in a variable.. For the next iteration then compare the values with that stored variable...

sample code:



Loop at itab into wa_itab.

check the combination of I and S.
if the value is differnt:
value = wa_itab.
else.
check the value of wa_itab > value.
if sy-subrc eq 0.
do what you want.
endif.
endif.
endloop.

Read only

venkat_o
Active Contributor
0 Likes
1,278

Hi Kani, Just do this way. It gives whatever u r expecting.

DELETE ADJACENT DUPLICATES FROM it_tab COMPARING <filed-1> <field-2>.
Thanks, Venkat.O

Read only

Former Member
0 Likes
1,279

Hi,

Here is the code which will help you.

DATA : begin of it_in OCCURS 0,

field1(2) TYPE c,

field2(2) TYPE c,

field3(2) TYPE c,

field4(3) TYPE c,

field5 TYPE i,

field6 TYPE i,

END OF it_in.

DATA wa_in LIKE LINE OF it_in.

DATA wa_in1 LIKE LINE OF it_in.

DATA : begin of it_out OCCURS 0,

field1(2) TYPE c,

field2(2) TYPE c,

field3(2) TYPE c,

field4(3) TYPE c,

field5 TYPE i,

field6 TYPE i,

END OF it_out.

DATA wa_out LIKE LINE OF it_out.

wa_in-field1 = 'l1'.

wa_in-field2 = 's1'.

wa_in-field3 = 'm1'.

wa_in-field4 = 'MS1'.

wa_in-field5 = '70'.

wa_in-field6 = '5'.

APPEND wa_in to it_in.

wa_in-field1 = 'l1'.

wa_in-field2 = 's1'.

wa_in-field3 = 'm1'.

wa_in-field4 = 'MS2'.

wa_in-field5 = '20'.

wa_in-field6 = '6'.

APPEND wa_in to it_in.

wa_in-field1 = 'l1'.

wa_in-field2 = 's2'.

wa_in-field3 = 'm2'.

wa_in-field4 = 'MS1'.

wa_in-field5 = '10'.

wa_in-field6 = '2'.

APPEND wa_in to it_in.

wa_in-field1 = 'l2'.

wa_in-field2 = 's2'.

wa_in-field3 = 'm3'.

wa_in-field4 = 'MS2'.

wa_in-field5 = '20'.

wa_in-field6 = '4'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm3'.

wa_in-field4 = 'MS3'.

wa_in-field5 = '10'.

wa_in-field6 = '8'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm4'.

wa_in-field4 = 'MS4'.

wa_in-field5 = '50'.

wa_in-field6 = '2'.

APPEND wa_in to it_in.

wa_in-field1 = 'l3'.

wa_in-field2 = 's3'.

wa_in-field3 = 'm5'.

wa_in-field4 = 'MS5'.

wa_in-field5 = '50'.

wa_in-field6 = '3'.

APPEND wa_in to it_in.

SORT it_in by field1 field2 field5.

LOOP AT it_in INTO wa_in.

IF sy-tabix eq 1.

MOVE wa_in to wa_in1.

else.

IF wa_in-field1 eq wa_in1-field1 AND wa_in-field2 eq wa_in1-field2.

IF wa_in-field5 eq wa_in1-field5.

IF wa_in1-field6 le wa_in-field6.

APPEND wa_in1 to it_out.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ELSE.

APPEND wa_in to it_out.

CLEAR wa_in.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ENDIF.

else.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

CONTINUE.

ENDIF.

ELSE.

APPEND wa_in1 to it_out.

CLEAR wa_in1.

MOVE wa_in to wa_in1.

ENDIF.

ENDIF.

ENDLOOP.

Read only

0 Likes
1,278

Hi Lekha ,

I tested . for one scenario it didnot give right o/p . But your code was very helpful.

Thanks.I have given you points.

Read only

0 Likes
1,278

Hi kani,

happy to hear that your issue closed..!

Did you check with my code...!

also just try to execute it once...!

It gives you the exact output as you mention in your thread....!

But its 100% useless because you were unable to mention your problem clearly,,,!

Anyways...! I am the one of them who simply wasted time..>!

-Dileep .C

Read only

Former Member
0 Likes
1,278

What Probs it has created.

Is your problem solved ? If not i will fix the prob ....