0

How to compare fields in same column in internal table? ABAP

Example to compare in column A:

col A | col B
 A    |   B
 A    |   A
 A    |   A
 B    |   B
 B    |   B
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Mykola
  • 93
  • 1
  • 3
  • 11
  • Excuse me, but I think something's wrong in your question... "two fields in same column" sounds weird. Did you mean row? – VXLozano Jul 03 '18 at 13:31
  • Yep, very weird. Give the proper structure of your table. – Suncatcher Jul 03 '18 at 14:39
  • @Mikola I imagine a scenario where the column is a structure. Is it possible that you post the internal table structure please? – Nelson Miranda Jul 03 '18 at 15:06
  • @ VXLozano I have to compare the fields in the column - not in row – Mykola Jul 04 '18 at 05:08
  • @ Nelson Miranda ok, I'll post it, but can we even compare the values in the column? – Mykola Jul 04 '18 at 05:15
  • @Suncatcher because the structure is partial – Mykola Jul 04 '18 at 11:00
  • 1
    Fields in the same column are **rows**, and columns are fields themselves. So your question sounds like nonsense. For making row-wise operations you need LOOP and compare, or GROUP BY. – Suncatcher Jul 04 '18 at 11:30
  • I suggest you to edit your question, changing "cols" by "rows", as, even in the answer you marked as correct, the objects compared are fields IN THE SAME ROW. In fact, FIELDS = COLUMNS and REGISTERS = ROWS in almost any data-related context, I bet. – VXLozano Jul 05 '18 at 09:38

1 Answers1

1

I would first loop through the contents of your internal table and do my comparison between field 1 and field 2 within the loop. The comparison is done on a row by row level. If the condition is true, I would add my business logic within the IF statement.

LOOP at itab.
   IF itab-col1 EQ itab-col2
   "Business logic.
   ENDIF. 
 ENDLOOP.

Would this suffice?

field-symbols: <ls_line> type (line structure of itab)

loop at itab assigning <ls_line>.
  if <ls_line>-column_a NE <ls_line>-column_b.
    write: / sy-tabix, <ls_line>-column_a, <ls_line>-column_b. 
  endif.
endloop.

sy-tabix will give the line number where there is a difference between the 2 columns.

  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Capricorn Jul 03 '18 at 18:51
  • @ Cameron Smith thanks from the answer but my question is how to compare the values in the column and can we generally compare the values in the column? – Mykola Jul 04 '18 at 05:20
  • @Mykola , ?I don't really understand what your question is. What I have supplied is a row by row comparison. Are you perhaps trying to do a column by column comparison? – Cameron Smith Jul 04 '18 at 09:55
  • @Cameron Smith yes, column by column – Mykola Jul 04 '18 at 10:09
  • @Mykola Is there a specific reason why you don't sort the internal table at first? – Cameron Smith Jul 04 '18 at 10:25
  • @Cameron Smith reason is I need a new list with the old order - without sorting (because this list will be used by other actions in the future - so it's important not to sort it) – Mykola Jul 04 '18 at 10:42
  • @Cameron Smith thank you very much I tried - that's what I needed ;-) – Mykola Jul 04 '18 at 11:34