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

count double records

Former Member
0 Likes
2,100

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,892

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

9 REPLIES 9
Read only

Mohamed_Mukhtar
Active Contributor
0 Likes
1,892

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

Read only

0 Likes
1,892

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

Read only

Former Member
0 Likes
1,893

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

Read only

Former Member
0 Likes
1,892

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.

Read only

agnihotro_sinha2
Active Contributor
0 Likes
1,892

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.

Read only

0 Likes
1,892

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.

Read only

Former Member
0 Likes
1,892

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

Read only

Former Member
0 Likes
1,892

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

Read only

awin_prabhu
Active Contributor
0 Likes
1,892

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.