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

ABAP Code not working

adeel_sarwar
Contributor
0 Likes
4,326

Hi,

I have made a query and in the query I have created an additional field to find any duplicates in a table. When I check the code there is no error which says to me the code is ok however when I run the query it brings a result of 0 even though there are duplicate production order numbers. What am I doing wrong?

Please see below code if you can change this and let me know i would appreciate this.

Points will be awarded thanks

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count(2) TYPE p.

DATA : no_of_records(3) TYPE p.

Clear no_of_records.

select aufnr from ekkn into itab.

endselect.

SORT itab BY aufnr.

LOOP AT itab.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

ADD count TO no_of_records.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

DUPLICATES = NO_of_records.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
4,042

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

data:BEGIN OF itab_final OCCURS 0,

aufnr LIKE ekkn-aufnr,

count type p decimals 0,

end of it_final.

data dup_cnt type p decimals 0.

data:wk_total type p decimasl 0.

sort itab by aufnr ascending.

loop at itab.

at first.

clear dup_cnt.

clear wk_total.

endat.

at new aufnr.

clear itab_final.

itab_final-aufnr = itab-aufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of aufnr.

if dup_cnt <= 1.

dup_cnt = dup_cnt - 1.

endif.

itab_final-count = dup_cnt.

append itab_final.

endat.

endloop.

Now itab_final holds the data...just print it...for sum of duplicate records

if u r using a normal report

at last.

sum itab_final-count.

write itab_final-count.

endat.

hope this solves all ur issues or else if u donot want to move it other itab just use my last post

i guess the majority of replies here are correct,,,,do try to debug yourself and find a solution bcoz 99 % of solution is posted by few experts here.

49 REPLIES 49
Read only

0 Likes
2,906

check your mail

Read only

0 Likes
2,906

Hi All,

Thanks for all your help code works now thanks keeshu especially and everyone else

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

CLEAR: Duplicates.

SELECT aufnr FROM ekkn INTO TABLE itab

WHERE aufnr = ekkn-aufnr.

SORT itab BY aufnr .

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 1.

DUPLICATES = COUNT.

CLEAR count.

else.

clear count.

DUPLiCATES = COUNT.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,906

for finding the duplicate count.

data:wk_lines type i,wk_lines2 type i,wk_dup type i.

sort itab ascending.

describe table itab lines wk_lines.

delete adjacent duplicates from itab comparing aufnr.

describe table itab lines wk_lines2.

wk_dup = wk_lines - wk_lines2.

write: 'no of duplicates record-' , wk_dup.

Read only

Former Member
0 Likes
2,906

Hi Friend,

can u tell me ur requirement clearly actually what u want to fetch from database. I checked the table ekkn from where u what to fetch data, for the field aufnr data is not available in the data base for any doc. no.

Read only

0 Likes
2,906

Hi Kavitha,

I think i might be explaining it wrong but Ill try again in more details.

I have a query using joing for the following tables EKPO, EKKN and EKKO. It pulls of a list of purchase orders which are linked to production orders (External Processing PO's)

Now there are multiple PO's raised for the same production order for one reason or another and I just wanted to run the query which displays like this. So what I wanted was the code for the Count/Duplicate Field which will look at EKKN-AUFNR and find any duplicates in that table as you can see below. Does this help? The field that I want to pass the value through is called DUPLICATES but everyone here is gicing write statements which show total list of duplicates.

Does this help? Thanks again for all your help and answer nearlly there in getting the result

Purch.Doc. Item Order Count/Duplicate

4500001258 00010 1031372 1

4500001259 00010 1031373 1

4500001260 00010 1031374 2

4500001261 00010 1031375 1

4500001262 00010 1031376 1

4500001263 00010 1031377 1

4500001264 00010 1031378 1

4500001265 00010 1031374 2

Read only

0 Likes
2,906

You might've extend your previous post instead posting new one with AUFNR ...every one got confused...

any way check this code...

DATA : BEGIN OF itab OCCURS 0,

ebeln LIKE ekko-ebeln, "PO number

banfn LIKE eban-banfn, "production order

count TYPE p DECIMALS 0,

END OF itab.

DATA : count(2) TYPE p.

DATA : no_of_records(3) TYPE p.

itab-banfn = '4500001258'.

itab-ebeln = '1031372'.

APPEND itab.

itab-banfn = '4500001259'.

itab-ebeln = '1031373'.

APPEND itab.

itab-banfn = '4500001260'.

itab-ebeln = '1031374'.

APPEND itab.

itab-banfn = '4500001261'.

itab-ebeln = '1031375'.

APPEND itab.

itab-banfn = '4500001262'.

itab-ebeln = '1031376'.

APPEND itab.

itab-banfn = '4500001263'.

itab-ebeln = '1031377'.

APPEND itab.

itab-banfn = '4500001264'.

itab-ebeln = '1031378'.

APPEND itab.

itab-banfn = '4500001265'.

itab-ebeln = '1031374'.

APPEND itab.

SORT itab BY ebeln.

LOOP AT itab.

ADD 1 TO count.

AT END OF ebeln.

IF count GE 2.

DO.

READ TABLE itab WITH KEY ebeln = itab-ebeln count = 0.

IF sy-subrc EQ 0.

itab-count = count.

MODIFY itab INDEX sy-tabix.

ELSE.

EXIT.

ENDIF.

ENDDO.

ELSE.

itab-count = count.

MODIFY itab INDEX sy-tabix.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

LOOP AT itab.

WRITE 😕 itab-banfn, itab-ebeln, itab-count.

ENDLOOP.

*NOTE: my code works only when PO created with ONLY 2 Reqistions..if 3 requisiotns it won't work..

BUtttttt...very rare case we create PO with 3 requisitions..

Edited by: Perez C on Jan 4, 2008 10:28 AM

Read only

0 Likes
2,906

just check my last post....it will give u the exact logic.

also include endloop in the last line of my code.

reward if usefull

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,906

just execute this program

data:begin of itab occurs 0,

wkaufnr(3),

end of itab.

data dup_cnt type i.

data:wk_total type i.

itab-wkaufnr = 100.

append itab.

itab-wkaufnr = 200.

append itab.

itab-wkaufnr = 100.

append itab.

itab-wkaufnr = 200.

append itab.

itab-wkaufnr = 300.

append itab.

itab-wkaufnr = 200.

append itab.

sort itab by wkaufnr ascending.

loop at itab.

at new wkaufnr.

write:/ itab-wkaufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of wkaufnr .

if dup_cnt > 1.

write at 15 dup_cnt left-justified.

wk_total = wk_total + dup_cnt.

else.

write at 15 '0' left-justified.

endif.

endat.

at last.

write:/ 'TOT.DUP'.

write at 15 wk_total left-justified.

endat.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,906

Just execute this code once.

&----


*& Report YMSEG *

*& *

&----


*& *

*& *

&----


report a.

data:begin of itab occurs 0,

wkaufnr(3),

end of itab.

data dup_cnt type i.

data:wk_total type i.

itab-wkaufnr = 100.

append itab.

itab-wkaufnr = 200.

append itab.

itab-wkaufnr = 100.

append itab.

itab-wkaufnr = 200.

append itab.

itab-wkaufnr = 300.

append itab.

itab-wkaufnr = 200.

append itab.

sort itab by wkaufnr ascending.

loop at itab.

at first.

clear dup_cnt.

clear wk_total.

write: 'List of Duplicate Entries'.

write :/1(20) sy-uline.

endat.

at new wkaufnr.

write:/ itab-wkaufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of wkaufnr .

if dup_cnt > 1.

write at 15 dup_cnt left-justified.

wk_total = wk_total + dup_cnt.

else.

write at 15 '0' left-justified.

endif.

endat.

at last.

write :/1(20) sy-uline.

write:/ 'Duplicates'.

write at 15 wk_total left-justified.

write :/1(20) sy-uline.

endat.

endloop.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
4,043

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

data:BEGIN OF itab_final OCCURS 0,

aufnr LIKE ekkn-aufnr,

count type p decimals 0,

end of it_final.

data dup_cnt type p decimals 0.

data:wk_total type p decimasl 0.

sort itab by aufnr ascending.

loop at itab.

at first.

clear dup_cnt.

clear wk_total.

endat.

at new aufnr.

clear itab_final.

itab_final-aufnr = itab-aufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of aufnr.

if dup_cnt <= 1.

dup_cnt = dup_cnt - 1.

endif.

itab_final-count = dup_cnt.

append itab_final.

endat.

endloop.

Now itab_final holds the data...just print it...for sum of duplicate records

if u r using a normal report

at last.

sum itab_final-count.

write itab_final-count.

endat.

hope this solves all ur issues or else if u donot want to move it other itab just use my last post

i guess the majority of replies here are correct,,,,do try to debug yourself and find a solution bcoz 99 % of solution is posted by few experts here.