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: 

Sum fields from two internal tables

antonio_bruno
Participant
0 Kudos

Hi,

I need to sum in an internal table (it_final) the quantity billed from the single positions of the sales order.

I've build two internal tables to extract the relevant sales order positions (ODVAP) and the subsequent billed positions (ODVFATT).

When I try to connect the two internal table into "it_final" the sum doesn't work. How i can fix the problem?

In the infoset, i've inserted in the Coding (used selection criteria DD, DV, etc.):

1 - DATA

DATA: begin of ODVFATT occurs 0,
VBELN like VBAK-VBELN,
POSNR2 like VBAP-POSNR,
ABGRU2 like VBAP-ABGRU,
NETWR2 like VBAP-NETWR,
VBELN3 like VBFA-VBELN,
POSNN3 like VBFA-POSNN,
VBTYP_N3 like VBFA-VBTYP_N,
FKIMG4 like VBRP-FKIMG,
NETWR4 like VBRP-NETWR,
end of ODVFATT,
begin of ODVAP occurs 0,
VBELN like VBAK-VBELN,
POSNR like VBAP-POSNR,
KWMENG like VBAP-KWMENG,
NETWR like VBAP-NETWR,
end of ODVAP.
TYPES: BEGIN OF ty_final, 
VBELN TYPE VBAP-VBELN, 
POSNR TYPE VBAP-POSNR, 
KWMENG TYPE VBAP-KWMENG,
NETWR TYPE VBAP-NETWR, 
FKIMG TYPE VBRP-FKIMG, 
NETWR2 TYPE VBRP-NETWR,
END OF ty_final.
DATA: it_final TYPE STANDARD TABLE OF ty_final, 
wa_final TYPE ty_final,
wa_ODVFATT like ODVFATT, 
wa_ODVAP like ODVAP.

2 - START-OF-SELECTION

select A~VBELN C~POSNR C~ABGRU C~NETWR D~VBELN D~POSNN D~VBTYP_N
F~FKIMG F~NETWR
into table ODVFATT
from VBAK as A inner join VBUK as B
on A~VBELN = B~VBELN
inner join VBAP as C
on A~VBELN = C~VBELN
inner join VBFA as D
on C~VBELN = D~VBELV
and C~POSNR = D~POSNV
inner join VBRK as E
on D~VBELN = E~VBELN
inner join VBRP as F
on D~VBELN = F~VBELN
and D~POSNN = F~POSNR
where A~VBELN IN DV
and A~AUDAT IN DD
and A~ERNAM IN UT
and A~AUART IN TODV
and A~VKORG IN ORG
and A~VTWEG IN CD
and A~KUNNR IN COM
and A~KVGR5 IN AM
and B~GBSTK NE 'A'
and D~VBTYP_N IN ('M', 'P', 'O')
and E~FKSTO = ''.
sort ODVFATT by VBELN POSNR2.
select A~VBELN B~POSNR B~KWMENG B~NETWR
into table ODVAP
from VBAK as A inner join VBAP as B
on A~VBELN = B~VBELN
where A~VBELN IN DV
and A~AUDAT IN DD
and A~ERNAM IN UT
and A~AUART IN TODV
and A~VKORG IN ORG
and A~VTWEG IN CD
and A~KUNNR IN COM
and A~KVGR5 IN AM
and B~ABGRU = ''.
SORT ODVAP BY VBELN POSNR.
LOOP AT ODVAP INTO wa_ODVAP.
READ TABLE ODVFATT INTO wa_ODVFATT WITH KEY VBELN = wa_ODVAP-VBELN 
POSNR2 = wa_ODVAP-POSNR BINARY SEARCH. 
wa_final-VBELN = wa_ODVAP-VBELN. 
wa_final-POSNR = wa_ODVAP-POSNR.
wa_final-KWMENG = wa_ODVAP-KWMENG. 
wa_final-NETWR = wa_ODVAP-NETWR. 
wa_final-FKIMG = wa_final-FKIMG + wa_ODVFATT-FKIMG4. 
wa_final-NETWR2 = wa_final-NETWR2 + wa_ODVFATT-NETWR4. 
APPEND wa_final TO it_final. 
CLEAR: wa_final, wa_ODVFATT, wa_ODVAP. 
ENDLOOP.

I've checked the table with an order with the following data.

sum-2-internal-tables.jpg


In my case, I need that the it_final-FKIMG = 18 + 4 not only 18
and NETWR2 = 519,84 + 115,52 not only 519,84.

Thanks and regards

1 ACCEPTED SOLUTION

bertrand_delvallee
Active Participant
0 Kudos

Hello,

I think you just need to use COLLECT instruction.

So change your code to something like :

* DATA: it_final TYPE STANDARD TABLE OF ty_final, 
DATA : it_final LIKE HASHED TABLE OF ty_final WITH UNIQUE KEY VBELN POSNR,

* Search for every values you have to ADD (in wa_final-NETWR2 and wa_final-FKIMG) and put :
COLLECT wa_final TO it_final.

* Don't forget to CLEAR fields after to prepare next operation
CLEAR : wa_final-FKIMG, wa_final-NETWR2.

* With COLLECT you don't have to double LOOP (LOOP in LOOP) if you don't need to. Just perform 2 LOOPS one after the other. 
3 REPLIES 3

horst_keller
Product and Topic Expert
Product and Topic Expert

As a rule, debugging can help ...

Furthermore, check if sy-subrc < > 0 after READ table. Do you really read all the data that you expect and if yes, what happens with them?

former_member306787
Active Participant
0 Kudos

Hello Antonio Bruno,

There are a few flaws in your logic, I'll try to explain them.

1. You shouldn't use READ TABLE, but use a LOOP instead.

2. Before you add a new entry to IT_FINAL, check if there isn't already one present. So do a READ TABLE on IT_FINAL and if SY-SUBRC = 0 => add the quantity fields. If SY-SUBRC <> 0 => add a new line.

Your coding should look like this:

LOOP AT ODVAP INTO wa_ODVAP.
  LOOP AT ODVFATT INTO wa_ODVFATT WHERE VBELN = wa_ODVAP-VBELN 
                                  AND   POSNR2 = wa_ODVAP-POSNR.
  "Check IT_FINAL 
  READ TABLE IT_FINAL ASSIGNING <FS_FINAL> with key VBELN = wa_ODVAP-VBELN 
                                                    POSNR2 = wa_ODVAP-POSNR. 

  IF SY-SUBRC = 0. "=>add quantity/value

  ELSE. "=> add new line to IT_FINAL

  ENDIF.

  ENDLOOP.
ENDLOOP.

bertrand_delvallee
Active Participant
0 Kudos

Hello,

I think you just need to use COLLECT instruction.

So change your code to something like :

* DATA: it_final TYPE STANDARD TABLE OF ty_final, 
DATA : it_final LIKE HASHED TABLE OF ty_final WITH UNIQUE KEY VBELN POSNR,

* Search for every values you have to ADD (in wa_final-NETWR2 and wa_final-FKIMG) and put :
COLLECT wa_final TO it_final.

* Don't forget to CLEAR fields after to prepare next operation
CLEAR : wa_final-FKIMG, wa_final-NETWR2.

* With COLLECT you don't have to double LOOP (LOOP in LOOP) if you don't need to. Just perform 2 LOOPS one after the other.