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

problem with collect

Former Member
0 Likes
1,325

Hello to all!

In sap I have a table, there are rows with the same name but with different quantity. I want to summarize this rows like this:

SELECT

   c~aufnr

   p~matnr p~bdter p~meins p~baugr p~dbskz p~erfmg p~aufnr

   f~maktx

        INTO CORRESPONDING FIELDS OF TABLE it_tab

        FROM  afpo AS c

          INNER JOIN resb AS p ON c~aufnr = p~aufnr

          INNER JOIN makt AS f ON p~matnr = f~matnr.

loop at it_tab into fs_tab.

     collect fs_tab into it_tab_collected.

endloop.

     it_tab = it_tab_collected.

But in this case it sums only absolutelly identically rows. I need to sum up rows only with the same name.

How can I achieve this?

Regards, Alexander.

Hello to all!

In sap I have a table, there are rows with the same name but with different quantity. I want to summarize this rows like this:

SELECT

   c~aufnr

   p~matnr p~bdter p~meins p~baugr p~dbskz p~erfmg p~aufnr

   f~maktx

        INTO CORRESPONDING FIELDS OF TABLE it_tab

        FROM  afpo AS c

          INNER JOIN resb AS p ON c~aufnr = p~aufnr

          INNER JOIN makt AS f ON p~matnr = f~matnr.

loop at it_tab into fs_tab.

     collect fs_tab into it_tab_collected.

endloop.

     it_tab = it_tab_collected.

But in this case it sums only absolutelly identically rows. I need to sum up rows only with the same name.

How can I achieve this?

Regards, Alexander.

10 REPLIES 10
Read only

Former Member
0 Likes
1,288

Hi,

Are both fields having the primary key?

Read only

Former Member
0 Likes
1,288

Hi,

The meaning of collect statement is.. it inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key.

if want to add fields with same name then use

ADD-CORRESPONDING struc1 TO struc2.

All components of the same name in struc1 and struct2 are added in pairs and the result is assigned to the respective component of struct2.


Regards

tejas

Read only

former_member183073
Active Participant
0 Likes
1,288

i dont think that the collect statement will check the name

of the fields before summing up. it will sum the numeric fields in the given itab only if the itab and wa has the same type and considers the keys in case of sorted and hashed table(it will sum only the non unique keys if the unique keys have the same values)

which field field are you trying to summarize here ERFMG?

Read only

0 Likes
1,288

Thats right I try to sum ERFMG. So in to my table i can select only numeric fields?

Read only

0 Likes
1,288

hi,

Yes, you can sum up only numeric fields on the basis of all common character fields

Regards

Tejas

Read only

0 Likes
1,288

the collect statement will sum only the numeric fields, i think aufnr and matnr will make a unique key in your table, if right, declare it_tab_collected as sorted table with unique key aufnr matnr. then use collect statement.

i think that should work.

Read only

0 Likes
1,288

Agree with Syed. U need to check the Aufnr and Matnr to be same. Let us know

Read only

former_member196651
Contributor
0 Likes
1,288

Hi Alex,

Yes. Using collect will add up only the numeric fields in the workarea whose key fields matches with the row already in the internal table.

Regards,

Abijith

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,288

Hello Alexander,

I think you should read the following guideline on COLLECT - ABAP Keyword Documentation.

If you think about the semantics of the COLLECT statement, you'll also agree that it should be used on HASHED tables(ideally) or SORTED tables(with unique primary key).

BR,

Suhas

Read only

thangam_perumal
Contributor
0 Likes
1,288

Hi Alex,

             Please refer below code....

DATA: BEGIN OF seats,
        carrid   TYPE sflight-carrid,
        connid   TYPE sflight-connid,
        seatsocc TYPE sflight-seatsocc,
      END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats
               WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc
       FROM sflight
       INTO seats.
  COLLECT seats INTO seats_tab.
ENDSELECT.




Regards,

  Thangam.P