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

Calculate sum internal table

Former Member
0 Likes
5,737

Hi experts,

At the end of an abap program, let’s say I have the following internal table:

Field1

Field2

Field3

Amount

TEST1

22/01/2013

A001

-412.80499

TEST2

15/03/2013

P001

  1. 2089.27519

TEST3

10/02/2012

A231

  1. 238.49557

TEST4

15/04/2013

PXXX

  1. 0.68680

I would like to perform some mathematic calculations on my table:

-Calculate Amount where field3(1) = A and  Amount where field3(1) = P and then display some messages regarding the result of calculations by comparing the 2 sums.

So in my case, the calculations will be as following:

Field3

Amount

A001

-412.80499

A231

238.49557

Sum 1

-174,30942

P001

2089.27519

PXXX

0.68680

Sum 2

2089,96199

Then I have to calculate Sum1 – Sum2 and display some messages based on the result.

How do you advice me to calculate the sums on my internal table?

Thanks for your support.

Amine

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,636

Hi Amine,

1. Sort the ITAB on Field3 before loop.

2. I guess, the LOOP -ENDLOOP code u wriiten works well, but not give the desired output u mention above.

        

Field3

Amount

A001

-412.80499

A231

238.49557

Sum 1

-174,30942

P001

2089.27519

PXXX

0.68680

Sum 2

2089,96199

the  sum amount will be added and displayed against the end of FIELD3, but another row of sum 1 is not added.

3. please let know whether field3 will hav values only starting with A and P?

Regards,

Azhar  

Hi experts,

At the end of an abap program, let’s say I have the following internal table:

Field1

Field2

Field3

Amount

TEST1

22/01/2013

A001

-412.80499

TEST2

15/03/2013

P001

  1. 2089.27519

TEST3

10/02/2012

A231

  1. 238.49557

TEST4

15/04/2013

PXXX

  1. 0.68680

I would like to perform some mathematic calculations on my table:

-Calculate Amount where field3(1) = A and  Amount where field3(1) = P and then display some messages regarding the result of calculations by comparing the 2 sums.

So in my case, the calculations will be as following:

Field3

Amount

A001

-412.80499

A231

238.49557

Sum 1

-174,30942

P001

2089.27519

PXXX

0.68680

Sum 2

2089,96199

Then I have to calculate Sum1 – Sum2 and display some messages based on the result.

How do you advice me to calculate the sums on my internal table?

Thanks for your support.

Amine

18 REPLIES 18
Read only

Former Member
0 Likes
3,636

Below 2 proposals, but I don’t know if they are good practices or not in my case…

Loop at ITAB into structure.

at end of structure-field3.

If structure-field3(1) = ‘A’.

sum. 

Endif.

endat.

Endloop.

LOOP at itab into structure where -field3(1) = ‘A’.

COLLECT Amount.

ENDLOOP


Thanks.

Amine

Read only

0 Likes
3,636

One thing you can do is create another column(FIELD4) in your table to store an indicator of the matching sets of rows you want to calculate. Then, it's easy to loop through them

Field4

Field2

Field3

Amount

A

22/01/2013

A001

-412.80499

P

15/03/2013

P001

  1. 2089.27519
                    A            10/02/2012                A231
  1. 238.49557
                    P            15/04/2013               PXXX
  1. 0.68680

I wouldn't subtotal inside your internal table. Loop through it and subtotal out into another table or onto whatever your output is going to be.

Read only

0 Likes
3,636

Loop at ITAB into structure.

at end of structure-field3.

If structure-field3(1) = ‘A’.

sum.

Endif.

endat.

Endloop.

first option is better one..Collect statement is absolute don't use it.

Read only

Former Member
0 Likes
3,637

Hi Amine,

1. Sort the ITAB on Field3 before loop.

2. I guess, the LOOP -ENDLOOP code u wriiten works well, but not give the desired output u mention above.

        

Field3

Amount

A001

-412.80499

A231

238.49557

Sum 1

-174,30942

P001

2089.27519

PXXX

0.68680

Sum 2

2089,96199

the  sum amount will be added and displayed against the end of FIELD3, but another row of sum 1 is not added.

3. please let know whether field3 will hav values only starting with A and P?

Regards,

Azhar  

Read only

0 Likes
3,636

Hi Azhar,

I confim that field3 will have only values starting with either A or P.

Amine

Read only

0 Likes
3,636

Hi Amine,

the below is the simple logic that i built..

loop at itab into wa_itab.
  if wa_itab-field3+0(1) = 'A'.
     sum_a = sum_a + wa_itab-field4.
  elseif wa_itab-field3+0(1) = 'P'.
     sum_p = sum_p + wa_itab+field4    
endloop.

sum_diff = sum_a - sum_p.

if sum_diff = 0.
  write:/ "difference is zero".
else.
  write:/ "difference is", sum_diff.

hope  this would do the business that u need.

Regards,

Azhar


 
endif.

Read only

0 Likes
3,636

Thanks Azhar,

In fact it's very simple and efficient

Amine

Read only

0 Likes
3,636

Hi amine,

I would do just like Azhar proposes, but instead of a new loop, can't you add this to the processing where you build your internal table? Avoiding an unnecessary loop is always good

Cheers,

Custodio

@zcust01

Read only

former_member197425
Active Participant
0 Likes
3,636

Hi,

Below logic may be useful in your case..

types: begin of itype,

            field1     type string,

            field2     type datum,

            field3(5) type C, "make sure field3 is of type c for alphabetic sorting.

            field4     type wogxxx,

       end of itype.

data : itab type table of itype,

         amt_A type wogxxx,

         amt_P type wogxxx,

         tot   type wogxxx.   

field-symbol: <fs> type itype.

......fill itab table with all data..

.....then follow below logic for summation..

        sort itab BY field3 AS TEXT. " this will group data alphabetically 

        loop at itab assigning <fs>.

           if <fs>-field3(1) = 'A'.

              amt_A = amt_A + <fs>-field4.

           elseif <fs>-field3(1) = 'P'.

             amt_P = amt_P + <fs>-field4.

           endif.

        endloop.

tot = amt_A - amt_P.

Thanks,

Nandi.

Read only

0 Likes
3,636

Thanks Nandi.

I like this logic, i will test it.

Amine

Read only

Former Member
0 Likes
3,636

Ok thanks guys.

And how can i do to use the obtained sums A and P in my program:

Field3

Amount

A001

-412.80499

A231

238.49557

Sum 1

-174,30942

P001

2089.27519

PXXX

0.68680

Sum 2

2089,96199

Actually, i want to calculate Sum1 and Sum2 internally in my program and then use them to display some messages as following:

SUM = Sum1+Sum2

If SUM = 0.

               write:'XXXX'

elseif SUM <> 0.

               write :'YYYY'.

Thanks.

Amine

Read only

0 Likes
3,636

Hi,

If you are trying to find the SUM of A's - SUM of P's, then you can use a single variable SUM to maintain.

the code snippet looks like this..

data: sum type wogxxx.

sum = 0.

loop at ITAB assigning <FS>.

if <fs>-field3(1) = 'A'.

sum = sum + <fs>-field4.

elseif <fs>-field3(1) = 'P'.

sum = sum - <fs>-field4.

endif.

if sum = 0.

write: 'sum is zero'.

else.

write: 'sum is non zero'.

endif.

Regards,

Karthik

Read only

0 Likes
3,636

Hi Karthik,

And what about if i want to show the diffeence between the sum of A and the Sum of P.

Let's say:

Sum A = 100

Sum P = 150

Difference = SUM A - SUM P

Difference = 100 - 150

Difference = (-50)

Thanks.

Amine

Read only

bastinvinoth
Contributor
0 Likes
3,636

Hi Amine

First option is better i think

Loop at ITAB into structure.

at end of structure-field3.

If structure-field3(1) = ‘A’.

sum.

Endif.

endat.

Endloop.

Regards,

Bastin.G

Read only

bastinvinoth
Contributor
0 Likes
3,636

Hi amine lamkaissi

By the By ,your way of presenting the question in forum always good   i like it


Read only

0 Likes
3,636

Thanks Bastin

Amine

Read only

Former Member
0 Likes
3,636

hi,

     This logic may help you.

sum = 0

loop at itab.

if itab-field3+0(1) eq  'A'.

sum = sum + itab-amount.

elseif itab-field3+0(1) eq  'P'.

sum = sum - itab-amount.

endif.

endloop.

if sum eq 0.

write / 'sum is zero'.

else.

write / 'sum is non zero'.

endif.

* if u want to display [ sum A - sum P]

if sum ne 0

write 😕 'Difference of sumA and sumP is :' ,sum.

endif.

Read only

Former Member
0 Likes
3,636

Hi Amine,

Try this code

DATA: lv_total_a TYPE i,   " Holds sum of field3 starting with 'A'

       lv_total_p TYPE i,   " Holds sum of field3 starting with 'P'

       lv_sum     TYPE i.   " Holds total sum

LOOP AT itab INTO wa.

   IF wa-field3(1) = 'A'.

     ADD wa-amount TO lv_total_a.

   ELSEIF wa-field3(1) = 'P'.

     ADD wa-amount TO lv_total_p.

   ENDIF.

   CLEAR wa.

ENDLOOP.

lv_sum = lv_total_a - lv_total_p.