Application Development 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: 

abap in SQ02. Sum functions.

Former Member
0 Kudos

Hello, i am new to abap in SQ01/SQ02 querys. I made a query which generates a list of the orders in table AUFK and looks in the billing document table VBRP to see if there is a credit amount involved in the order. I made a simple code which generates a correct result, but it takes to much time, and i cannot run the query for the amount of orders i need to look up.

The code i wrote is as follows:

select sum( netwr ) into creditamount from vbrp

Where aufnr = aufk-aufnr

And vgtyp = 'K'.

This code seems to work fine, but it takes to long to generate an output for the whole list for a year. Each order takes about 10 seconds to generate.

So then i tried to do the same, but then by using an internal table. I think this could speedup the runtime significantly.

I think i succesfully managed to create the internal table and fill it with data, but I am not sure how to pull data from the internal table into to a field I made in the infoset (fieldname = creditamount).

What i tried is the following:

types: begin of ty_itab,

netwr type vbrp-netwr,

vgtyp type vbrp-vgtyp,

aufnr type vbrp-aufnr,

end of ty_itab.

data: wa_itab type ty_itab.

DATA : BEGIN OF itab OCCURS 0,

netwr type vbrp-netwr,

vgtyp type vbrp-vgtyp,

aufnr type vbrp-aufnr,

END OF itab.

select netwr vgtyp aufnr from vbrp into itab.

endselect.

sort itab by vgtyp.

loop at itab into wa_itab where aufnr = aufk-aufnr.

if wa_itab-vgtyp ='k'.

sum.

write creditamount.

endif.

endloop.

unfortunately, this code returns nothing. Any suggestions?

Thanks in advance.

5 REPLIES 5

pranay570708
Active Contributor
0 Kudos

Hi,

Use below code:

types: begin of ty_itab,

netwr type vbrp-netwr,

vgtyp type vbrp-vgtyp,

aufnr type vbrp-aufnr,

end of ty_itab.

data: itab type standard table of ty_itab,

          wa_itab type ty_itab.

select netwr vgtyp aufnr from vbrp into table itab

                                                Where aufnr = aufk-aufnr

                                                And vgtyp = 'K'.

if sy-subrc = 0.

loop at itab into wa_itab.

creditamount = creditamount + wa_itab-netwr.   "Sum up for all records

clear wa_itab.

endloop.

endif.

write: creditamount.

0 Kudos

I tried that code. It seems to work fine if i lookup a single order, but when looking up a list it doesn't return the right value (often multiple times the same value in the list).


I do not know much about internal tables, but i think the internal table is not properly deleting or writing the right value at each line of the order list. Thanks for the answer.  Now in the meanwhile , i will try to see if i can somehow correct the output.

0 Kudos

HI,

Try putting a clear statement before the Loop.

clear creditamount.

loop at itab into wa_itab.

creditamount = creditamount + wa_itab-netwr. 

clear wa_itab.

endloop.

endif.

Former Member
0 Kudos

I got it working. Thanks Pranay, i couldn't have done this without your help. Below i pasted the final working code i used. There might be unnessisary coding in there, such as collect itab, but this code worked for me.

The query took around 27 hours to run for around 15.000 orders, but was able to return correct data. I think this is just due to the amount of calculations the system has to do.

The coding i used:

types: begin of ty_itab,

netwr type vbrp-netwr,

vgtyp type vbrp-vgtyp,

aufnr type vbrp-aufnr,

end of ty_itab.

data: itab type standard table of ty_itab with header line,

      wa_itab type ty_itab.

select netwr vgtyp aufnr from vbrp into table itab

                                                Where aufnr = aufk-aufnr

                                                And vgtyp = 'K'.

clear creditamount.

collect itab.

loop at itab into wa_itab.

creditamount = creditamount + wa_itab-netwr.   "Sum up for all records

clear wa_itab.

endloop.

write: creditamount.

0 Kudos

Hi,

You can also try below to save time of execution:

select netwr vgtyp aufnr from vbrp into table itab

                                                Where aufnr = aufk-aufnr

                                                And vgtyp = 'K'.

clear creditamount.

loop at itab into wa_itab.

sum.

creditamount = wa_itab-netwr.   "Sum up for all records

clear wa_itab.

exit.

endloop.