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

storing sum in variable

Former Member
0 Likes
2,758

hii,

there is date field in table and value against each date

for ex.

date value

10.01.2008 2

16.01.2008 1

20.01.2008 2

10.02.2008 3

17.02.2008 1

30.02.2008 2

i want to store the sum of values for every month in different variables .

like..

month 1

var1 = 5

month 2

var2 = 6

Thanks

14 REPLIES 14
Read only

Former Member
0 Likes
1,728

try using collect statment

Read only

Former Member
0 Likes
1,728

Hi,

loop at the itab. use at first / at last / sum and put the result in a new itab with same structure.

Use the following code

DATA: BEGIN OF lt_vbap,

mandt type vbap-mandt,

vbelv type vbap-vbelv,

posnv type vbap-posnv,

vbeln type vbap-vbeln,

posnn type vbap-posnn,

rfmng_flo type vbap-rfmng_flo,

END OF lt_vbap.

DATA itab_vbap LIKE HASHED TABLE OF lt_vbap

WITH UNIQUE KEY MANDT VBELV POSNV VBELN POSNN .

SELECT MANDT VBELV POSNV VBELN POSNN RFMNG_FLO

FROM VBAP

INTO lt_vbap.

COLLECT lt_vbap INTO itab_vbap.

ENDSELECT.

let me know if this solves your problem...

Regds,

Manas

Read only

0 Likes
1,728

Hii,

wat i did is

IF wal-begda1+4(2) = '01'.

wam-sl = wal-abwtg.

sl1 = sl1 + wam-sl.

ENDIF.

is this correct...

Read only

0 Likes
1,728

Dear Akshaya,

You are doing right thing. in adition you can use the Fm

' CACS_DATE_GET_YEAR_MONTH'

for calculating month and if possible use collect so your value will be added with respect to month automatically.

Regard,

Vijay

Read only

Former Member
0 Likes
1,728

Hi Akshaya,

Is your problem resolved?

Rgds,

Manas

Read only

0 Likes
1,728

hey manas,its not solved yet

i appended the data to final table.

Read only

Former Member
0 Likes
1,728

Collect statement wont work in your case.

In ur internal table u have 2 fields , add one more field to it.

That is month, date and value

itab-month date value

01 10.01.2008 2

01 16.01.2008 1

01 20.01.2008 2

02 10.02.2008 3

02 17.02.2008 1

02 30.02.2008 2

Your month shd be the first field.

Sort itab my month.

declare itab_final with 2 fields month and value.

declare wa_itab of line of itab_final.

loop at itab into wa.

at new month.

clear wa_final.

endat.

move-corresponding wa into wa_final.

wa_final-value = wa-value + wa_final-value.

at endof month.

append wa_final to itab_final.

endat.

endloop.

Read only

Former Member
0 Likes
1,728

IF date+4(2) = '01'.

value = value1.

sum = sum+value1.

ENDIF.

append at the end to final internal table

Read only

0 Likes
1,728

is it resolved akshaya...?

Praise the help indeed...

Rgds,

Manas

Read only

0 Likes
1,728

ok let me try

Read only

Former Member
0 Likes
1,728

Hi Akshaya,

DATA: BEGIN OF itab OCCURS 0,

date TYPE sy-datum,

value TYPE i,

END OF itab.

DATA: v_sum TYPE i,

v_mon(2) TYPE n,

v_temp(2) TYPE n.

itab-date = '20090101'.

itab-value = 12.

APPEND itab.

itab-date = '20090112'.

itab-value = 15.

APPEND itab.

itab-date = '20090203'.

itab-value = 10.

APPEND itab.

itab-date = '20090215'.

itab-value = 3.

APPEND itab.

READ TABLE itab INDEX 1.

IF sy-subrc = 0.

v_mon = itab-date+4(2).

ENDIF.

LOOP AT itab.

v_temp = itab-date+4(2).

IF v_mon NE v_temp.

WRITE:/5 v_mon.

WRITE: 15 v_sum.

CLEAR v_sum.

v_mon = v_temp.

v_sum = v_sum + itab-value.

ELSE.

v_sum = v_sum + itab-value.

ENDIF.

ENDLOOP.

WRITE:/5 v_mon.

WRITE: 15 v_sum.

CLEAR v_sum.

Regards,

Kumar Bandanadham

Edited by: Velangini Showry Maria Kumar Bandanadham on May 28, 2009 8:49 AM

Read only

Former Member
0 Likes
1,728

hi

you can make another itab(say ita1) which wil store month and value

now make a variable say sum of type i.

now loop at ur itab into wa

if sum is initial.

sum = wa-val.

wa1-month = wa-date+4(2).

elseif wa-date4(2) eq wa1-date4(2).

sum = sum + wa-val.

else.

wa1-val = sum.

append wa1 to itab1.

clear sum.

clear wa1.

endif.

append wa1 to itab1.

plz check the syntex as i dont have sap right now.

hope this help

Edited by: mayank jain on May 28, 2009 8:51 AM

Read only

Former Member
0 Likes
1,728

Hi,

*use this syntax...

*pass the data to character mode:

loop at it.

at new f1.

sum.

endat.

endloop.

Naveen M.

Read only

Former Member
0 Likes
1,728

Hi,

First sort the internal table1 with the date.

Create an internal table2 with month and total value.

Then

loop at internal table1 into workarea1.

if workarea1-date+4(2) ne v_tmp.

if sy-tabix ne 1.

move v_tmp to work area2.

move v_value to work area2.

append workarea2 to IT2.

endif.

v_value = workarea1-value.

v_tmp = workarea1-date+4(2).

else.

v_value = workarea1-value + v_value.

v_tmp = workarea1-date+4(2).

endif.