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: 

Calculate SUM based in condition in iTAB

Former Member
0 Kudos

Hi all,

I have the ITAB as follows.

wa_mseg-mblnr = '5000000130'.

wa_mseg-mjahr = '2008'.

wa_mseg-bwart = '901'.

wa_mseg-dmbtr = '00000005000'.

wa_mseg-bpmng = '00000000100'.

wa_mseg-ebeln = '1059200855'.

wa_mseg-ebelp = '00010'.

APPEND wa_mseg to itab_mseg.

wa_mseg-mblnr = '5000000131'.

wa_mseg-mjahr = '2008'.

wa_mseg-bwart = '902'.

wa_mseg-dmbtr = '00000002500'.

wa_mseg-bpmng = '00000000050'.

wa_mseg-ebeln = '1059200855'.

wa_mseg-ebelp = '00010'.

APPEND wa_mseg to itab_mseg.

wa_mseg-mblnr = '5000000132'.

wa_mseg-mjahr = '2008'.

wa_mseg-bwart = '901'.

wa_mseg-dmbtr = '00000002500'.

wa_mseg-bpmng = '00000000050'.

wa_mseg-ebeln = '1059200855'.

wa_mseg-ebelp = '00010'.

APPEND wa_mseg to itab_mseg.

now i want to add the BPMNG for the BWART = 901

DMBTR for the BWART = 901

ans same thing for the BWART = 902.

how do i calculate the SUM based on condition.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

now i want to add the BPMNG for the BWART = 901

DMBTR for the BWART = 901

ans same thing for the BWART = 902.

how do i calculate the SUM based on condition.

Hi, you can loop de internal table and do a control cut by "bwart". For doing that the order of the fields of your internal table must change.

If you have:

1st.mblnr

2nd.mjahr

3rd.bwart

you have to change to:

1st.bwart

2nd.mblnr

3rd.mjahr

So in the loop you will can do like this.

loop at itab.
aux_sum = itab-BPMNG + aux_sum.
at end of bwart.
* Here you will have de SUM for BWART.
* Then you clear aux_sum for the next different BWART.
endat.
endloop.

hope this help you.

Andrew83

10 REPLIES 10

naimesh_patel
Active Contributor
0 Kudos

Explore the help on the COLLECT statement.

You can take a temporary table with the required field and use the COLLECT to get the SUM.

Regards,

Naimesh Patel

Former Member
0 Kudos

now i want to add the BPMNG for the BWART = 901

DMBTR for the BWART = 901

ans same thing for the BWART = 902.

how do i calculate the SUM based on condition.

Hi, you can loop de internal table and do a control cut by "bwart". For doing that the order of the fields of your internal table must change.

If you have:

1st.mblnr

2nd.mjahr

3rd.bwart

you have to change to:

1st.bwart

2nd.mblnr

3rd.mjahr

So in the loop you will can do like this.

loop at itab.
aux_sum = itab-BPMNG + aux_sum.
at end of bwart.
* Here you will have de SUM for BWART.
* Then you clear aux_sum for the next different BWART.
endat.
endloop.

hope this help you.

Andrew83

former_member194797
Active Contributor
0 Kudos

BWART must be the first col. of itab_mseg.

code:


sort itab_mseg by BWART.

loop at itab_mseg.

at end of bwart.
  SUM.
  WRITE: / 'BWART:', itab_mseg-BWART,
           'BPMNG:', itab_mseg-BPMNG,
           'DMBTR:', itab_mseg-DMBTR,
endat.

endloop.

Former Member
0 Kudos

Hi,

Try this.

sort the table by key field and bwart.

loop at itab into wa.

s_bpmng = s_bpmng + wa-bpmng.

s_dmbtr = s_dmbtr + wa-dmbtr.

at end of bwart.

<sum of bpmng for a particular bwart will be there in s_bpmng

sum of dmbtr for a particular bwart will be there in s_dmbtr>

<here do the logic according to ur requirement.>

clear: s_bpmng, s_dmbtr.

endat.

endloop.

Sharin.

former_member194797
Active Contributor
0 Kudos

With the SUM command, there is no need of additional variables.

Subhankar
Active Contributor
0 Kudos

Hi...

after populating the ITAB sort the table by bwart

then

loop at ITAB into wa_mseg.

v_sum = v_sum + wa_mseg-bpmng .

v_DMBTR = v_DMBTR + wa_mseg-DMBTR.

write : /

wa_mseg-mblnr

wa_mseg-mjahr

wa_mseg-bwart

wa_mseg-dmbtr

wa_mseg-bpmng

wa_mseg-ebeln

wa_mseg-ebelp .

at end of bwart.

write:/ v_sum, v_DMBTR

endat.

endloop.

Edited by: Subhankar Garani on Sep 16, 2008 12:20 PM

Former Member
0 Kudos

KR,

Use collect <internal table name> statement.

Former Member
0 Kudos

Hi,

You do like this.

data : sum type i,

sum1 type i.

loop at it_mseg into wa_mseg where bwart = '901'.

sum = sum + wa_mseg-bpmng.

sum1 = sum1 + wa_mseg-dmbtr.

endloop.

Thanks.

Former Member
0 Kudos

And you write,

write 😕 'SUM = ', sum,

'SUM1 =', sum1.

Bye.

Former Member
0 Kudos

use COLLECT statement with taking default key BWART field.