Application Development and Automation 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: 
Read only

column enyries

former_member630092
Participant
0 Likes
1,456

how to compare column entries in itab.

i want to count no of repeating entries in one column by comparing value to next one? want to print count and repeating value. any idea ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,415

hi

create an itab2 which have 2 fields field1 type your field and field2 as count type i and a work area wa2 for itab2.

create an count variable type i.

short itab by field.

now loop at your itab

if wa2 is initial.

wa2-field = itab-field.

count = count + 1.

elseif wa2-field eq itab-field.

count = count + 1.

else.

wa2-count = count.

append wa2 to itab2.

clear wa2.

endif.

endloop.

hope this help

how to compare column entries in itab.

i want to count no of repeating entries in one column by comparing value to next one? want to print count and repeating value. any idea ?

14 REPLIES 14
Read only

Former Member
0 Likes
1,415

Suppose itab has 2 columns a and b.

itab-a and itab-b.

Loop at itab into wa.

if wa-a = wa-b.

count = count + 1.

endif.

endloop.

Read only

Former Member
0 Likes
1,415

HI,

Try using DO..VARYING statement and get the values for the other colums of the same and compare.

Read only

Sm1tje
Active Contributor
0 Likes
1,415

The way I read it, is that Ravi wants to compare the values with each other from ONE and the same column, and count the number of different entries within this column???

If so, sort this table by this particular column, LOOP AT itab, and use AT NEW statement to count the number of entries.

Read only

0 Likes
1,415

sir, i tried using at new...endat. and showing all numbers once , but not able to get it. and how to link count with these numbers .any hint

itab-f1 has

100

100

200

200

300

it should display 100 is 2 times ,200 is 2 times and 300 is onetime.

Read only

Sm1tje
Active Contributor
0 Likes
1,415

ITAB-field1 has entries:

100

100

100

200

200

200

300

300

300

300


TYPES: BEGIN OF ty_itab,
  field1 TYPE char3.
TYPES: END OF ty_itab.

DATA: itab     TYPE TABLE OF ty_itab,
      workarea TYPE ty_itab,
      count    TYPE i.

workarea-field1 = '100'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '100'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '200'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '200'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '200'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '300'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '300'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '300'.
APPEND workarea TO itab.
CLEAR  workarea.

workarea-field1 = '300'.
APPEND workarea TO itab.
CLEAR  workarea.

LOOP AT itab INTO workarea.

  AT NEW field1.
    CLEAR: count.
  ENDAT.

  count = count + 1.

  AT END OF field1.
    WRITE:/1 workarea-field1, count.
  ENDAT.

ENDLOOP.

Edited by: Micky Oestreich on Jun 4, 2009 10:40 AM

Read only

Former Member
0 Likes
1,415

Sort it and use AT NEW field event...ad save that entry and increment count if entry exists....

Read only

0 Likes
1,415

sort the table by the column u want to check.

describe table itab lines lv_lines.

loop at itab into is1.
*if u just want to check the next entry, u can save the sy-tabix in a variable and increase it by 1. and read the 
*same table with this increased index.
      lv_tabix = sy-tabix + 1.
      clear is2.
      check lv_tabix le lv_lines. " to avoid going to dump when u are looping at last line. 
      read table itab into is2 index lv_index. " now u get the entries in next row.
      if sy-subrc = 0.
*         do ur comparison between is1 and is2. ex:
          if is1-xyz = is2-xyz.
............................
.............................
         endif.
     endif.
   clear : is1, is2.
endloop.

Read only

Former Member
0 Likes
1,416

hi

create an itab2 which have 2 fields field1 type your field and field2 as count type i and a work area wa2 for itab2.

create an count variable type i.

short itab by field.

now loop at your itab

if wa2 is initial.

wa2-field = itab-field.

count = count + 1.

elseif wa2-field eq itab-field.

count = count + 1.

else.

wa2-count = count.

append wa2 to itab2.

clear wa2.

endif.

endloop.

hope this help

Read only

Former Member
0 Likes
1,415

and also clear count after 'clear wa2.'

Read only

awin_prabhu
Active Contributor
0 Likes
1,415

Hi Ravi,

Try like below.

REPORT ztest.

DATA: BEGIN OF itab OCCURS 0,

matnr TYPE matnr,

END OF itab.

DATA: BEGIN OF itab1 OCCURS 0,

matnr TYPE matnr,

count TYPE i,

END OF itab1,

wa LIKE itab,

wa1 LIKE itab1,

c TYPE i VALUE 1,

v_matnr TYPE matnr.

wa-matnr = 100.

APPEND wa TO itab.

wa-matnr = 100.

APPEND wa TO itab.

wa-matnr = 200.

APPEND wa TO itab.

wa-matnr = 200.

APPEND wa TO itab.

wa-matnr = 300.

APPEND wa TO itab.

SORT itab BY matnr.

LOOP AT itab INTO wa.

AT NEW matnr.

c = 1.

ENDAT.

IF sy-tabix NE 1.

IF wa-matnr = v_matnr.

c = c + 1.

ENDIF.

AT END OF matnr.

wa1-matnr = wa-matnr.

wa1-count = c.

APPEND wa1 TO itab1.

ENDAT.

ENDIF.

v_matnr = wa-matnr.

ENDLOOP.

LOOP AT itab1 INTO wa1. "<--- Table 'itab1' contains 'matnr' and 'count'.

WRITE:/ wa1-matnr, wa1-count.

ENDLOOP.

Read only

Former Member
0 Likes
1,415

Hi,

Please make use of this LOGIC.

DATA: BEGIN OF itab OCCURS 0,

num(3),

END OF itab,

v_1(2),

v_2(2).

itab-num = '100'.

APPEND itab.

CLEAR itab.

itab-num = '100'.

APPEND itab.

CLEAR itab.

itab-num = '200'.

APPEND itab.

CLEAR itab.

itab-num = '200'.

APPEND itab.

CLEAR itab.

itab-num = '300'.

APPEND itab.

CLEAR itab.

LOOP AT itab.

AT NEW num.

v_1 = sy-tabix.

ENDAT.

AT END OF num.

v_2 = sy-tabix.

+v_2 = v_2 - v_1 + 1.+

* Here we write the entry and count.

WRITE : / itab-num,v_2 ,'times'.

ENDAT.

ENDLOOP.

Regards,

Smart Varghese.

Read only

Former Member
0 Likes
1,415

one more thing

you need to append wa2 to itab2 after endloop because the last count will not append in loop...endloop.

Read only

Former Member
0 Likes
1,415

Hi,

Please make use of this LOGIC.

DATA: BEGIN OF itab OCCURS 0,

num(3),

END OF itab,

v_1(2),

v_2(2).

itab-num = '100'.

APPEND itab.

CLEAR itab.

itab-num = '100'.

APPEND itab.

CLEAR itab.

itab-num = '200'.

APPEND itab.

CLEAR itab.

itab-num = '200'.

APPEND itab.

CLEAR itab.

itab-num = '300'.

APPEND itab.

CLEAR itab.

LOOP AT itab.

AT NEW num.

v_1 = sy-tabix.

ENDAT.

AT END OF num.

v_2 = sy-tabix.

v_2 = v_2 - v_1 + 1.

* Here we write the entry and count.

WRITE : / itab-num,v_2 ,'times'.

ENDAT.

ENDLOOP.

Regards,

Smart Varghese.

Read only

former_member630092
Participant
0 Likes
1,415

Thanks for support. Question is Answered .