07-31-2013 9:18 PM
Hello Gurus,
We are having one custom table and am picking the data from that table into itab, itab contains the values like below
field1 field2 field3 field4 field5 field6
A071458 Retail 0005002694 1200 Y010 1200000000
CMR1202576 CMR 0005002694 1100 Y001 1100000000
CMR1706299 CMR 0005002694 1100 Y001 1100000000
CMR3001127 CMR 0005002694 1100 Y001 1100000000
CMR3001991 CMR 0005002694 1100 Y001 1100000000
L018879 Retail 0005002694 1100 Y003 1100000000
from the above internal table i have to fetch the field1 starting with CMR30 and i have to pass the highest value from them into one variable. Means
field1
CMR3001127
CMR3001991 from these i have to find highest value and pass it to another variable. As i am fresher am not able to find the solution.
Please light me on this.
Thanks and Regards,
Muralikrishna Peravali
08-01-2013 12:38 PM
You can sort the internal table in descending order and read first record that matches pattern.
Something like this:
SORT itab BY field1 DESCENDING.
LOOP AT itab INTO workarea.
IF workarea-field1+0(5) EQ 'CMR30'.
"the value you want.
EXIT.
ENDIF.
ENDLOOP.
08-01-2013 12:38 PM
You can sort the internal table in descending order and read first record that matches pattern.
Something like this:
SORT itab BY field1 DESCENDING.
LOOP AT itab INTO workarea.
IF workarea-field1+0(5) EQ 'CMR30'.
"the value you want.
EXIT.
ENDIF.
ENDLOOP.
08-01-2013 3:44 PM
Hi Manish Kumar,
Thankyou for the response. Actually in that table we have Cusotmer number. So based on Customer number will your code work?
Please advise me.
Thanks and Regards,
Muralikrishna Peravali
08-02-2013 8:03 AM
It should work.
You can try it out and post the result and code if it doesn't work.
08-02-2013 8:33 AM
Hi MuraliKrishna,
You can also try using the below logic.
TYPES : BEGIN OF typ_test,
char(10),
END OF typ_test.
DATA : wa TYPE typ_test,
itab TYPE TABLE OF typ_test,
high(10).
wa-char = 'CMR3001127'.
APPEND wa TO itab.
wa-char = 'CMR3001991'.
APPEND wa TO itab.
wa-char = 'CMS3001992'.
APPEND wa TO itab.
wa-char = 'CMR3001992'.
APPEND wa TO itab.
LOOP AT itab INTO wa WHERE char+0(5) = 'CMR30'.
IF wa-char+5(5) > high+5(5).
high = wa-char.
ENDIF.
ENDLOOP.
WRITE : high.
***************************
output : CMR3001992
Regards,
Archana
08-02-2013 8:38 AM
That would be really handy when internal table has millions of records.
08-02-2013 11:30 AM
Hi Muralikrishna,
It is working fine.... follow this way....
TYPES : BEGIN OF ty_itab,
a(20) TYPE c,
b(20) TYPE c,
END OF ty_itab.
DATA : it_itab TYPE TABLE OF ty_itab,
wa_itab TYPE ty_itab.
wa_itab-a = 'CMR1202576'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR3001127'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR3001991'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR1706299'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
LOOP AT it_itab INTO wa_itab.
IF wa_itab-a+0(5) NE 'CMR30'.
delete it_itab .
ENDIF.
ENDLOOP.
SORT it_itab BY a DESCENDING.
clear wa_itab.
read table it_itab into wa_itab INDEX 1.
WRITE : / wa_itab-a.
08-02-2013 12:00 PM
hi
1. sort itab by descending
2. read table i.e first in itab is a biggest value
Suresh