2009 Jun 04 7:19 AM
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 ?
2009 Jun 04 9:34 AM
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 ?
2009 Jun 04 7:22 AM
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.
2009 Jun 04 7:24 AM
HI,
Try using DO..VARYING statement and get the values for the other colums of the same and compare.
2009 Jun 04 7:27 AM
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.
2009 Jun 04 9:23 AM
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.
2009 Jun 04 9:39 AM
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
2009 Jun 04 7:29 AM
Sort it and use AT NEW field event...ad save that entry and increment count if entry exists....
2009 Jun 04 7:51 AM
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.
2009 Jun 04 9:34 AM
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
2009 Jun 04 9:36 AM
2009 Jun 04 9:37 AM
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.
2009 Jun 04 9:38 AM
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.
2009 Jun 04 9:39 AM
one more thing
you need to append wa2 to itab2 after endloop because the last count will not append in loop...endloop.
2009 Jun 04 9:40 AM
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.
2009 Jun 05 4:28 AM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |