‎2008 May 02 1:43 PM
Hi Experts,
I have an internal table having records. I want to check each record of the table with every other record to find out if there exist same record for more then one key field.
Can anybody help me to place the logic for how i can check the records with each record? Like
lifnr banks
1 12
2 34
3 12
4 12
5 76
I want to find out 1,3 and 4.
Thanks a ton in advance
‎2008 May 02 1:47 PM
Hi Moni,
Try the Below Piece of Code:
DATA:
jtab LIKE itab.
jtab = itab.
LOOP AT itab INTO wa.
LOOP AT jtab INTO wa1 WHERE banks EQ wa-banks.
write:
/ wa-banks.
ENDLOOP.
ENDLOOP.OR
Try the Below Piece of Code what it does is Fetches the First record and Compares it with all the other Records:
DATA: w_index TYPE i,
w_lines TYPE i.
DESCRIBE itab LINES w_lines.
LOOP AT itab INTO wa.
w_index = sy-tabix + 1.
DO.
READ TABLE itab INTO wa1 INDEX w_index.
IF w_index <= w_lines.
IF wa-banks EQ wa1-banks.
write:
/ wa-banks.
ENDIF.
ELSE.
EXIT.
ENDIF.
ADD 1 TO w_index.
ENDDO.
ENDLOOP.Regards,
Sunil.
‎2008 May 02 1:47 PM
Hi Moni,
Try the Below Piece of Code:
DATA:
jtab LIKE itab.
jtab = itab.
LOOP AT itab INTO wa.
LOOP AT jtab INTO wa1 WHERE banks EQ wa-banks.
write:
/ wa-banks.
ENDLOOP.
ENDLOOP.OR
Try the Below Piece of Code what it does is Fetches the First record and Compares it with all the other Records:
DATA: w_index TYPE i,
w_lines TYPE i.
DESCRIBE itab LINES w_lines.
LOOP AT itab INTO wa.
w_index = sy-tabix + 1.
DO.
READ TABLE itab INTO wa1 INDEX w_index.
IF w_index <= w_lines.
IF wa-banks EQ wa1-banks.
write:
/ wa-banks.
ENDIF.
ELSE.
EXIT.
ENDIF.
ADD 1 TO w_index.
ENDDO.
ENDLOOP.Regards,
Sunil.
‎2008 May 02 2:02 PM
Hi Sunil,
Thanks for the reply. can you also tell me that if suppose 2 and 4 is duplicate of 1 and i need to fill a duplicate field of 1 with 2 and 3.
How can I do it?
Means
lifnr banks duplicate
1 xx 2, 3
2 xx 3 etc
Something like that
‎2008 May 02 2:14 PM
Hi Moni,
If there exists a third field called Duplicate (of type Char or STRING)and if you want to fill it with the Duplicates, If yes, then try the Foll Code:
DATA:
jtab LIKE itab,
w_duplicate(10) TYPE c.
jtab = itab.
LOOP AT itab INTO wa.
LOOP AT jtab INTO wa1 WHERE banks EQ wa-banks.
CONCATENATE w_duplicate wa1-lifnr INTO w_duplicate SEPARATED BY SPACE.
wa-duplicate = w_duplicate.
MODIFY itab FROM wa.
ENDLOOP.
ENDLOOP.
LOOP AT itab INTO wa.
write:
/ wa-lifnr,
wa-banks,
wa-duplicates.
ENDLOOP.Hope that was related to your Doubt.
Regards,
Sunil.
Edited by: Sunil Saini on May 2, 2008 3:15 PM
‎2008 May 02 2:22 PM
Actually i have that Duplicate field in the table where I am storing all the matching records. In that case how I can modify that.
When I am filling it with itab-duplicate = wa1-lifnr. where all other fields are initializing with wa then it is doing but not in an ordered manner
Mean in one it is showing 4 and in 4 showing 1.
Thanks for Help Sunil, But Cna you sort this too?
‎2008 May 02 2:34 PM
Hi Moni,
IF in my First Reply the Second technique was working then this should solve your Problem:
DATA:
w_index TYPE i,
w_lines TYPE i,
w_duplicate(10) TYPE c.
DESCRIBE itab LINES w_lines.
LOOP AT itab INTO wa.
w_index = sy-tabix + 1.
DO.
READ TABLE itab INTO wa1 INDEX w_index.
IF w_index <= w_lines.
IF wa-banks EQ wa1-banks.
CONCATENATE w_duplicate wa1-lifnr INTO w_duplicate SEPARATED BY SPACE.
wa-duplicate = w_duplicate.
MODIFY itab FROM wa.
ENDIF.
ELSE.
EXIT.
ENDIF.
ADD 1 TO w_index.
ENDDO.
ENDLOOP.
LOOP AT itab INTO wa.
write:
/ wa-lifnr,
wa-banks,
wa-duplicates.
ENDLOOP.ELSE try the below code it is same as above except one more Internal Table is used:
DATA:
w_index TYPE i,
w_lines TYPE i,
jtab LIKE itab,
w_duplicate(10) TYPE c.
DESCRIBE itab LINES w_lines.
LOOP AT itab INTO wa.
w_index = sy-tabix + 1.
DO.
READ TABLE jtab INTO wa1 INDEX w_index.
IF w_index <= w_lines.
IF wa-banks EQ wa1-banks.
CONCATENATE w_duplicate wa1-lifnr INTO w_duplicate SEPARATED BY SPACE.
wa-duplicate = w_duplicate.
MODIFY itab FROM wa.
ENDIF.
ELSE.
EXIT.
ENDIF.
ADD 1 TO w_index.
ENDDO.
ENDLOOP.
LOOP AT itab INTO wa.
write:
/ wa-lifnr,
wa-banks,
wa-duplicates.
ENDLOOP.Regards,
Sunil.
‎2008 May 02 2:54 PM
Hi Sunil,
your first code is working for me. But when I am putting the concatenate stat it is showing
lifnr duplicate
1 2
2 2
4 2
in my case 1 = 2 and 2 = 4 or 1 = 2, 4 something like that
Can you help in this lastly.......?
‎2008 May 02 3:37 PM
Hi Moni,
I don't think that it will work using First Code because we have used LOOP with WHERE condition so it will take all the Records starting from First to Last.
So for this only Second Code(Technique) can Solve the Problem.
Regards,
Sunil.
‎2008 May 02 1:51 PM
Hi Moni,
just try this logic
du_itab[] = itab[] . * store all the table records into another table*
loop at itab into wa. " here one record comes into workarea
read table du_itab into du_wa with key du_wa-number = wa-number.
*( here read command checks in the table du_itab if any same record exits and that record puts into the du_wa)***
endloop.
just try like above u can get..
Regards,
Surya
‎2008 May 02 1:52 PM
Hi Moni,
Try this sample code, wherein we can seperate the duplicated records and store them in a seperate internal table.
***************************************************************
LOOP AT it_data INTO wa_data.
index_1 = sy-tabix.
index_1 = index_1 + 1.
LOOP AT it_data INTO wa_data FROM index_1
WHERE plant = wa_data-plant
AND material = wa_file_data1-zmaterial
AND fisc_year = wa_file_data1-zfisc_year.
MOVE-CORRESPONDING wa_data TO wa_data1.
APPEND wa_data1 TO it_data1.
CLEAR: wa_data, wa_data1.
flag = c_01.
IF flag = c_01.
flag = 0.
index_1 = index_1 - 1.
n1_i = n1_i + c_01.
ENDIF.
ENDLOOP.
ENDLOOP.
***************************************************************
Hope this is helpful to you. If you need further information, revert back.
Reward all the helpful answers.
Regards
Nagaraj T
‎2008 May 02 2:00 PM
This works
DATA:
BEGIN OF rec1,
lifnr(1),
bank(2),
END OF rec1,
itab LIKE STANDARD TABLE OF rec1.
DATA:
BEGIN OF rec2,
bank(2),
count TYPE i,
END OF rec2,
itab2 LIKE STANDARD TABLE OF rec2.
START-OF-SELECTION.
PERFORM build_table.
REFRESH itab2.
LOOP AT itab INTO rec1.
rec2-bank = rec1-bank.
rec2-count = 1.
COLLECT rec2 INTO itab2.
ENDLOOP.
LOOP AT itab2 INTO rec2
WHERE count GT 1.
LOOP AT itab INTO rec1
WHERE bank EQ rec2-bank.
WRITE:/ 'Bank', rec1-bank, 'Lifnr', rec1-lifnr.
ENDLOOP.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form build_table
*&---------------------------------------------------------------------*
FORM build_table.
rec1-lifnr = '1'.
rec1-bank = '12'.
APPEND rec1 TO itab.
rec1-lifnr = '2'.
rec1-bank = '34'.
APPEND rec1 TO itab.
rec1-lifnr = '3'.
rec1-bank = '12'.
APPEND rec1 TO itab.
rec1-lifnr = '4'.
rec1-bank = '12'.
APPEND rec1 TO itab.
rec1-lifnr = '5'.
rec1-bank = '76'.
APPEND rec1 TO itab.
ENDFORM. " build_table
and the Output
Bank 12 Lifnr 1
Bank 12 Lifnr 3
Bank 12 Lifnr 4
‎2008 May 02 2:19 PM
Hi,
Use the below logic.
data: begin of itab occurs 0,
lifnr type lifnr,
banks type i,
end of itab.
data: v_count type i,
v_banks type i.
data wa like itab.
itab-lifnr = 1.
itab-banks = 12.
append itab.
itab-lifnr = 2.
itab-banks = 34.
append itab.
itab-lifnr = 3.
itab-banks = 12.
append itab.
itab-lifnr = 4.
itab-banks = 12.
append itab.
itab-lifnr = 5.
itab-banks = 76.
append itab.
sort itab by banks.
read table itab index 1.
v_banks = itab-banks.
loop at itab.
if not v_banks = itab-banks.
v_banks = itab-banks.
write:/5 wa-lifnr, 20 wa-banks, 40 'Count = ', v_count.
clear v_count.
endif.
wa = itab.
v_count = v_count + 1.
endloop.
write:/5 wa-lifnr, 20 wa-banks, 40 'Count = ', v_count.