2013 Mar 13 11:30 AM
Hi,
I have 2 columns in a internal table as below ..can anyone suggest a logic which gives good performance as the entries in the internal table are huge.
Col1 Col2
22 XYZ
22 XYZ
22 XYZ
23 YTR
23 YTR
24 ABC
25 SDG
25 SDG
I want to run the above IT in a loop and re-arrange the above columns as below ..
Col1 Col2
22 XYZ
22
22
23 YTR
23
24 ABC
25 SDG
25
The first occurrence in column 1 should have the text in Column 2 and remaining rows should be cleared if the rows match.
Regrads
Praneeth
2013 Mar 13 12:31 PM
Hi Praneeth,
you can do the following:
data: l_col2 like it-col2.
sort it by col1 col2.
loop at it assinging <it>.
if l_col2 = <it>-col2.
clear <it>-col2.
else.
l_col2 = <it>-col2.
endif.
endloop.
Regards,
Guilherme Frisoni
2013 Mar 13 11:33 AM
hello,
if you detele duplicate entries then you can use
Delete adjacent duplicates from it comparing col2...
Thanks
Sabyasachi
2013 Mar 13 11:36 AM
Hi,
I do not want to delete the row because there are other columns in the IT so cannot use delete adjacent duplicates keyword. I only want to clear the content in column 2.
Regards
Praneeth
2013 Mar 13 11:40 AM
Hi Praneeth,
For better performance, Use Cursors Logic.
Search for the existing information available.
Search with the keyword "Parallel Cursors"
Read the F1 help on LOOP stmt using FROM and TO additions.
Hope this helps.
2013 Mar 13 11:45 AM
Hi Praneeth,
I NEED SMALL CLARIFICTAION ABOUT UR DATA.
If below data exist than what it will display.
Col1 Col2
22 XYZ
22 XYZ
22 ABC
23 YTR
23 XYX
24 ABC
25 SDG
25 SDG
25 ABC
If Internal Table contains baove data than what will be the OutPut.
Please rpely me than i will give u the Code.
Thanks
Tarak
2013 Mar 13 11:54 AM
Hi,
Each row is unique . So 25 ABC will not occur as 25 is already related to SDG. It means the value in col 1 is always linked to a unique value in col 2.
Regards
Praneeth
2013 Mar 13 12:14 PM
Hi,
Below code will arrange your table like you asked:
DATA: v_temp TYPE wa-f2.
SORT it BY f1 f2.
LOOP AT it INTO wa.
IF wa-f2 EQ v_temp.
CLEAR wa-f2.
MODIFY it FROM wa.
ELSE.
v_temp = wa-f2.
ENDIF.
ENDLOOP.
2013 Mar 13 12:21 PM
Hello praneeth, it = it1.
wa = wa1.
LOOP AT IT INTO WA.
AT NEW COL2.
MOVE-CORRESPONDING WA TO WA1.
APPEND WA1 TO IT1.
CONTINUE.
ENDAT.
CLEAR WA-COL2.
MOVE-CORRESPONDING WA TO WA1.
APPEND WA1 TO IT1.
ENDLOOP.
Thanks
Sabyasachi
2013 Mar 13 12:31 PM
Hi Praneeth,
you can do the following:
data: l_col2 like it-col2.
sort it by col1 col2.
loop at it assinging <it>.
if l_col2 = <it>-col2.
clear <it>-col2.
else.
l_col2 = <it>-col2.
endif.
endloop.
Regards,
Guilherme Frisoni
2013 Mar 13 3:28 PM
Hi,
For me algorithm from Guilherme is the simplest and the fastest if you are saying that source table is huge:
- It iterates once through source loop and updates values in place - just read row and update col2 if needed.
- We need to read all rows at least once to check if col2 from row must be left or it should be cleared. This algorithm reads each row only once.
- It uses assigning so in case of large table is important for performance - data is directly updated.
- It does not need any additional tables/structures, only single l_col2 variable as temporal one.
There is no need to sort also by col2, col1 sorted order is enough.
If values of col2 can be duplicated for different col1 values, like:
col1 = 22 col2 = ABC
col1 = 22 col2 = ABC
col1 = 23 col2 = ABC
... then in the loop if condition must refer to col1 instead of col2:
IF l_col1 = <it>-col1.
...
Regards,
Adam
2013 Mar 13 12:53 PM
Hi,
You can do the following:
sort <fs_tab> by col1 col2.
loop at <fs_tab> assigning <fs_wa>.
at new col2.
lv_new = c_x.
endat.
if lv_new ne c_x.
clear <fs_wa>-col2.
endif.
at end of col2.
clear lv_new.
endat.
endloop.
Regars,
Kartik
2013 Mar 13 2:45 PM
Hi Praneth,
here's my coding for your problem. But make certain, that your internal table is of type standard table (not hashed)
FIELD-SYMBOLS: <fs_ls>, <fs_lp>, <fs_c1s>, <fs_c1p>, <fs_c2s>.
DATA: l_ix TYPE i VALUE 1.
...
READ TABLE it INDEX l_ix ASSIGNING <fs_lp>.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_lp> TO <fs_c1p>.
ADD 1 TO l_ix.
READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
DO.
WHILE <fs_c1s> EQ <fs_c1p>.
CLEAR <fs_c2s>.
ADD 1 TO l_ix.
READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
IF sy-subrc NE 0.
EXIT.
ENDIF.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
ENDWHILE.
READ TABLE it INDEX l_ix ASSIGNING <fs_lp>.
IF sy-subrc NE 0.
EXIT.
ENDIF.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_lp> TO <fs_c1p>.
ADD 1 TO l_ix.
READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
ENDDO.