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

GETTING SUM

Former Member
0 Likes
659

HI EXPERTS,

I HAVE ONE INTERNAL TABLE HAVING ONE FIELD FILLED WITH VALUES FROM ONE TABLE.

SUPPOSE THE FIRST FIELD CONTAINS VALUES 1, -2, 3 , 1, 3, -4.

BASED ON THE ABOVE VALUES I NEED TO CALCULATE ONE VALUE THAT IS G_SUM1 = SUM OF ALL FIELD VALUES WHICH ARE GREATER THAN ZERO.

G_SUM2 = SUM OF ALL FIELD VALUES WHICH R LESS THAN ZERO.

THIS TWO SUMS SHOULD BE DISPLAYED AS THE SUMMARY OF THE REPORT.

COULD SOME PLZ SUGGEST ME HOW TO DO THIS CALCULATION.

REGARDS,

SIRI.

5 REPLIES 5
Read only

abdul_hakim
Active Contributor
0 Likes
633

you could try the below logic.....

LOOP AT ITAB.

IF ITAB-VAL1 GT 0.

G_SUM1 = G_SUM1 + ITAB-VAL1.

ELSEIF ITAB-VAL1 LT 0.

G_SUM2 = G_SUM2 + ITAB-VAL1.

ENDIF.

ENDLOOP.

END-OF-PAGE.

WRITE:/ G_SUM1,

G_SUM2.

Cheers,

Abdul Hakim

Read only

Laxmana_Appana_
Active Contributor
0 Likes
633

Hi,

Check this code :

x_sum1 and x_sum2 are the variables after data process.

loop at i_tab.

if i_tab-fld1 > 0.

x_sum1 = x_sum1 + i_tab-fld1.

elseif i_tab-fld1 < 0.

x_sum2 = x_sum2 + i_tab-fld1.

endloop.

x_sum1 will contain sum of all positive values.

x_sum2 will contain sum of all negetive values.

regards

Appana

Read only

Former Member
0 Likes
633

Loop at itab.

if itab-qty >= 0.

g_sum1 = itab-qty + g_sum1.

else.

g_sum2 = itab-qty + g_sum2.

endif.

endloop.

write : / g_sum1, g_sum2.

Read only

uwe_schieferstein
Active Contributor
0 Likes
633

Hello Sireesha

I prefer to use the COLLECT statement to calculate sums.

In your case, you would need to define to additional itabs:

DATA:
  gt_itab_positive   TYPE ...,
  gt_itab_negative   TYPE ... .

  LOOP AT gt_itab INTO ls_entry.
 
   IF ( ls_entry-value > 0 ).
     COLLECT ls_entry INTO gt_itab_positive.
   ELSE.
     COLLECT ls_entry INTO gt_itab_negative.
   ENDIF.

  ENDLOOP.

Regards

Uwe

Read only

Former Member
0 Likes
633

Hi sireesha,

you can declare ur internal table as sorted table with non unique key specfying ur fields as keys leaving the fields u want to sum(these fields should be specifically numeric).

And then when u want to append ur data use collect statement on the internal table.

Regards,

DS