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

Aggregate

Former Member
0 Likes
1,268

Hi,

I have an internal table with different fields:Material, Grade, Country of origin, Region and Qty. I need to aggregate the data per grade, country and region...Can anyone help?

13 REPLIES 13
Read only

Former Member
0 Likes
1,233

HI Mel,

Use the statement

collect itab.

in your code to get the aggregate as i can see all the fields other than quantity are non numeric.

Regards,

Ravi

Read only

0 Likes
1,233

Hi ravi,

but the thing is I need to aggregate based on the 4 fields I mentionned....

Read only

0 Likes
1,233

what you probably need is the use of the AT NEW control breaks in the loop .. endloop of your internal table..

Good Luck

Suresh Datti

Read only

0 Likes
1,233

COLLECT will do exactly that as long as all your character type fields are same, it will sum up all your number fields.

So instead of doing an APPEND itab, do a COLLECT itab.

Srinivas

Read only

Former Member
0 Likes
1,233

Mel,

use

loop at itab.

At new grade.

like that maintain all ur conditions and do collect itab.

Don't forget to sort the itab first.

Read only

Former Member
0 Likes
1,233

Hi,

This is sample code where you do an aggregate.

As per requirement, you read with the field you need the aggregate.

loop....

read table research_recap with key rcode = wrk_rec-rcode.

if sy-subrc = 0.

research_recap-reason_cnt = research_recap-reason_cnt + lv_cnt.

research_recap-ord_vol = research_recap-ord_vol + lv_ord_vol.

research_recap-inv_vol = research_recap-inv_vol + lv_inv_vol.

research_recap-netwr = research_recap-netwr + lv_netwr.

modify research_recap index sy-tabix.

else.

research_recap-reason_cnt = lv_cnt.

research_recap-ord_vol = lv_ord_vol.

research_recap-inv_vol = lv_inv_vol.

research_recap-netwr = lv_netwr.

append research_recap.

clear : research_recap.

endif.

endloop.

Regards,

Sailaja.

Read only

0 Likes
1,233

Hi,

i was wondering...how do u do AT NEW for different fields since I need to aggreagate per Grade, Country and region and not just by one field.

Read only

0 Likes
1,233

Hi Mel,

To make the aggregation based on three fileds,

Make these the first three fields in the internal table.

You should sort the internal table by the three fields.

Then use <b>at end of</b> for the third field.

sort itab bye grade country region.

loop at itab.

at end of region.

collect itab.

endat.

endloop.

Regards,

Ravi

Read only

Former Member
0 Likes
1,233

SELECT Material, Grade, Country of origin, Region

SUM( Qty ) AS bdmng

INTO CORRESPONDING FIELDS OF itab

FROM <table>

GROUP BY Material, Grade, Country of origin, Region .

hope this will work.

Allot points if your query solved.

Read only

0 Likes
1,233

Sorry Vinod but your query is not working!!!

Read only

0 Likes
1,233

Hi mel,

Try other alternatives with the same query like use QTY instead of BDMNG...

What I sent you is a pseudo code but the idea is the same . Check out as it worked for me many times.

Warm regards,

Vinod.

Read only

Former Member
0 Likes
1,233

Hi use the following coe.

sort itab by grade.

loop at itab.

collect itab.

endloop.

the above code will help u to achive ur functionality.

satish

Read only

0 Likes
1,233

Mel,

Can you please explain why COLLECT is not working? What is the structure of your internal table? May be that will help us to see why COLLECT is not working for you. COLLECT should be done when you are building the internal table, not when you already have the records in the table. Also, if you are selecting from database into this table directly, then the collect will not work. Is this the case with your internal table?

Please let us know.

Srinivas