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: 

Two table comparisions (logic problem)

Karan_Chopra_
Active Participant
0 Kudos

Suppose I have a table itab1 with two cols

col1         col2
A            0001
A            0002
A            0004
B            0001
B            0002
B            0003
B            0004

second table itab2 has values

0001

0002

0003

0004

i want to delete all rows from itab1 based on itab 2 which does not hav all the values in itab1 from itab2 based on cl1 n col2 both

e.g in this case only this shud remain

B            0001
B            0002
B            0003
B            0004

Edited by: Karan Chopra on Jun 21, 2010 3:16 PM

Edited by: Karan Chopra on Jun 21, 2010 3:18 PM

9 REPLIES 9

Former Member
0 Kudos

Hi,

Karan wrote...


second table itab2 has values 

0001
0002
0003
0004

i want to delete all rows from itab1 based on itab 2 which does not hav all the values in itab1 from itab2 based on cl1 n col2 both 
e.g in this case only this shud remain

But Itab2 does not have cl1 n col2 .

Can you please clarify me on this point.

Regards

DKS

Karan_Chopra_
Active Participant
0 Kudos

Yes itab 2 does not hav col1 value s

the thing is all the values of itab2 here shud exist in itab1 for same col1 value

as here col1 value B has all 0001 to 0004 values so it is considered

0 Kudos

Hi Karan,

This code will move the records from itab1 to itab3 where the value of col2 is present in itab2


data: lw_tabix type sytabix.
* Declare itab3 like itab1
loop at itab1 into wa_1.
* Search the value of col2 in table itab1
read table itab2 into wa_2 with key col1 = wa_1-col2.
if sy-subrc eq 0.
* if found then move the record to itab3
Move wa_2 to wa_3.
append wa_3 to itab3
endif.
endloop.

Now itab3 will hold the desired records

This will solve your problem...

Regards

DKS

former_member536879
Active Contributor
0 Kudos

Hi,

Please search the SCN before Posting. Many threads available in SCN related to this and refer the SCN rules before Posting.

With Regards,

Sumodh.P

Karan_Chopra_
Active Participant
0 Kudos

But on what criteria to search SCN

as this is very specific thing and I donot by what search criteria I should search

0 Kudos

Hello

Try anything like this:


data: begin of itab1 occurs 0,
      col1(1),
      col2(4),
      end of itab1.
data: begin of itab2 occurs 0,
      col2(4),
      end of itab2.
data: itab3 like itab2 occurs 0 with header line.
data: begin of itab4 occurs 0,
      col1(1),
      end of itab4.
itab1-col1 = 'A'. itab1-col2 = '0001'. append itab1.
itab1-col1 = 'A'. itab1-col2 = '0002'. append itab1.
itab1-col1 = 'A'. itab1-col2 = '0004'. append itab1.
itab1-col1 = 'B'. itab1-col2 = '0001'. append itab1.
itab1-col1 = 'B'. itab1-col2 = '0002'. append itab1.
itab1-col1 = 'B'. itab1-col2 = '0003'. append itab1.
itab1-col1 = 'B'. itab1-col2 = '0004'. append itab1.

itab2-col2 = '0001'. append itab2.
itab2-col2 = '0002'. append itab2.
itab2-col2 = '0003'. append itab2.
itab2-col2 = '0004'. append itab2.

sort: itab1, itab2.

loop at itab1.
  at new col1.
    refresh itab3.
  endat.
  itab3-col2 = itab1-col2. append itab3.
  at end of col1.
    sort itab3.
    if itab3[] <> itab2[].
      clear itab4. itab4-col1 = itab1-col1.
      append itab4.
    endif.
  endat.
endloop.
loop at itab4.
  delete itab1 where col1 = itab4-col1.
endloop.

loop at itab1.
  write: itab1-col1, itab1-col2.
  new-line.
endloop.

0 Kudos

Hi,

Try this logic

type : begin of ty_tab3,
         col1 type itab1-col1.
       end of ty_tab3.
data : itab3 type standard table of ty_tab3.
       wa3   type ty_tab3.
l_no(5) type n.
l_rec(5) type n.
l_col1 type itab1-col1.
delete adjacent duplicates from itab1 comparing all fields.
describle table itab2 lines l_no.
sort itab1 by col1.
move 0 to l_rec.
loop at itab1 into wa.
   if col1 ne l_col1.
      if l_rec eq l_no.
        clear : wa3, l_rec.
	move l_col1 to wa3-col1.
        append wa3 to itab3.
      endif.
   endif.
   add 1 to l_rec.
   move wa-col1 to l_col1.   	   
endloop.
if l_rec eq l_no.
  clear : wa3, l_rec.
  move l_col1 to wa3-col1.
  append wa3 to itab3.
endif.
loop at itab3 into wa3.
   loop at itab1 where col1 = wa3-col.
     write:/ itab1-col1 itab1-col2.
   endloop.
endloop.

Regards

Vinod

0 Kudos

Try this logic:


* Populate the master key based on ITAB2, before that SORT ITAB2
SORT ITAB2.
CONCATENATE LINES OF itab2 INTO v_master_key.

* Remember to SORT ITAB1
SORT itab1 BY col1 col2.

LOOP AT itab1.
  itab3 = itab1.
  APPEND itab3.
  CONCATENATE v_key itab1-col2 INTO v_key.
  AT END OF col1.
    CONDENSE v_key NO-GAPS.
    IF v_key NE v_master_key.
*     We're not interested in these records, delete them      
      DELETE itab3 WHERE col1 = itab1-col1.
    ENDIF.
    CLEAR: v_key. "Don't forget to clear the key
  ENDAT.
ENDLOOP.

LOOP AT itab3.
  WRITE: / itab3.
ENDLOOP.

BR,

Suhas

PS: I took Dzed's code as reference & tweaked it as per my logic. Apologies to Dzed

Former Member
0 Kudos

Hi ,

Please elaborate your question . From your description your query is not clear.

Do you want to delete the first occurrence of repeated entries from first table?

Please specify so that I can help you.

Regards,

Uma Dave