2009 May 05 2:07 PM
hi all,
how to count double records from internal table
E.G. have table with material number and wont to count number or material which exit more then one
1123
1124
1123
1126
1212
1124
I have 2 double matnr
2009 May 05 2:17 PM
Hi Nick,
* suppose itab1 contains duplicate entries of matnr.
itab2[] = itab1[].
delete adjacent duplicates from itab2 comparing matnr.
* now check the entries in both the tables ( lin1 and lin2 ) , you will find the difference.
DESCRIBE TABLE itab1 LINES lin1.
DESCRIBE TABLE itab2 LINES lin2.
Regards,
Aby
hi all,
how to count double records from internal table
E.G. have table with material number and wont to count number or material which exit more then one
1123
1124
1123
1126
1212
1124
I have 2 double matnr
2009 May 05 2:13 PM
hi,
Declare a counter
sort it by matnr
data Cnt type i value 0.
loop at it into wa.
ADD 1 TO cnt. "--> cnt holds no. of times the same material is repeated
at new matnr .
cnt = 1.
endat.
at end of matnr.
write : / wa-matnr , cnt.
endat.
endloop.Thanks & REgards
Always Learner
2009 May 05 2:20 PM
data : begin of t_matnr occurs 0,
matnr type matnr,
count type i,
end of t_matnr.
first sort the table It.
data : Counnt type i value 0.
loop at it into wa.
count = counnt + 1.
at end of matnr .
matnr = it-matnr.
append t_amtnr.
counnt = 0.
endat.
endloop.
the new internal tbale t_matnr contains the Material number and number of times the material number occurs.
regards,
Prabhudas
2009 May 05 2:17 PM
Hi Nick,
* suppose itab1 contains duplicate entries of matnr.
itab2[] = itab1[].
delete adjacent duplicates from itab2 comparing matnr.
* now check the entries in both the tables ( lin1 and lin2 ) , you will find the difference.
DESCRIBE TABLE itab1 LINES lin1.
DESCRIBE TABLE itab2 LINES lin2.
Regards,
Aby
2009 May 05 2:18 PM
try like this ..may be this works,
sort by matnr.
get the no of records in the internal table .
delete duplicate adjacents by matnr.
take the count of the no of records and get the difference with the previous which yields the no
of duplicated records.
2009 May 05 2:21 PM
hi,
Use 2 LOOP statements.
Before that transfer rec into another temp table IT2.
data: cnt type i.
LOOP at IT1 into wa1.
LOOP at IT2 into wa2 where F1 = wa1-F1.
cnt = cnt + 1.
endloop.
endloop.
Note: Final count total will be always LESS THAN 1 i.e. FINAL count = COUNT - 1.
ags.
2009 May 06 10:53 AM
hi Nick,
I am sorry for the wrong code. I missed a line in this code.
Please note that you have to delete the DUPLICATE recs first like given below:
Use 2 LOOP statements.
Before that transfer rec into another temp table IT2.
Then DELETE DUPLICATES from IT1. then run the loop.
data: cnt type i.
LOOP at IT1 into wa1.
LOOP at IT2 into wa2 where F1 = wa1-F1.
cnt = cnt + 1.
endloop.
endloop.
Note: Final count total will be always LESS THAN 1 i.e. FINAL count = COUNT - 1.
ags.
2009 May 05 2:21 PM
Hi,
once u got the data in the internal table.
than sort the internal table.
ie. sort <internal_table> by <material_number>.data:
w_mat type mara-matnr. " temp work variable to store the material number
w_count type i,
w_cnt type i.
loop at internal_table into work_area.
if w_mat eq work_area-matnr.
add 1 to w_count.
else.
add 1 to w_cnt.
endif.
w_mat = work_area-matnr.
endloop.
write 'total repeated materila number', w_count.
write: 'total records' , w_cnt.
hope this helps....
thanks
ravi
2009 May 05 2:22 PM
Hi Nick,
Check the bellow code -
DATA : w_value TYPE i,
BEGIN OF fs_itab ,
fld1 TYPE i,
END OF fs_itab,
t_itab LIKE TABLE OF fs_itab,
w_num TYPE i.
fs_itab-fld1 = 1.
APPEND fs_itab TO t_itab.
fs_itab-fld1 = '2'.
APPEND fs_itab TO t_itab.
fs_itab-fld1 = '1'.
APPEND fs_itab TO t_itab.
fs_itab-fld1 = '3'.
APPEND fs_itab TO t_itab.
fs_itab-fld1 = '4'.
APPEND fs_itab TO t_itab.
fs_itab-fld1 = '4'.
APPEND fs_itab TO t_itab.
SORT t_itab BY fld1. " Sorting the table
LOOP AT t_itab INTO fs_itab.
IF fs_itab-fld1 = w_value.
w_num = w_num + 1. " Contains the double no
ENDIF.
w_value = fs_itab-fld1.
ENDLOOP.
WRITE : 'Double number' , w_num.Regards
Pinaki
2009 May 05 3:02 PM
Try like below.
Table itab1 contains 2 fields 'matnr' and 'count'.
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 = 1123.
APPEND wa TO itab.
wa-matnr = 1124.
APPEND wa TO itab.
wa-matnr = 1123.
APPEND wa TO itab.
wa-matnr = 1126.
APPEND wa TO itab.
wa-matnr = 1212.
APPEND wa TO itab.
wa-matnr = 1124.
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.
AT END OF matnr.
wa1-matnr = wa-matnr.
wa1-count = c.
APPEND wa1 TO itab1.
ENDAT.
ENDIF.
ENDIF.
v_matnr = wa-matnr.
ENDLOOP.
LOOP AT itab1 INTO wa1. <--- Table 'itab1' contains 'matnr' and 'count'.
WRITE:/ wa1-matnr, wa1-count.
ENDLOOP.
Thanks.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |