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

Help with a COLLECT statement.

Former Member
0 Likes
1,206

I had to make changes to some code and these changes required me to add some fields to an internal table. Below is what the table looked like before I made any changes:

DATA : BEGIN OF t_frgroup OCCURS 0,

  • BEGIN CHANGE 02/11/03

hazmat TYPE c,

  • END CHANGE 02/11/03

mfrgr LIKE lips-mfrgr,

brgew LIKE lips-brgew,

lfimg LIKE lips-lfimg,

qtypal LIKE w_nbr_palletsx,

qtypce LIKE w_nbr_palletsx,

vstel LIKE likp-vstel,

no_cnvrt TYPE c,

END OF t_frgroup.

This is what it looked like after I made the changes:

DATA : BEGIN OF t_frgroup OCCURS 0,

  • BEGIN CHANGE 02/11/03

hazmat TYPE c,

  • END CHANGE 02/11/03

mfrgr LIKE lips-mfrgr,

brgew LIKE lips-brgew,

lfimg LIKE lips-lfimg,

qtypal LIKE w_nbr_palletsx,

qtypce LIKE w_nbr_palletsx,

vstel LIKE likp-vstel,

no_cnvrt TYPE c,

matnr TYPE lips-matnr,

vbeln TYPE lips-vbeln,

posnr TYPE lips-posnr,

qty LIKE vblkp-lfimg,

vrkme LIKE lips-vrkme,

converted(1) TYPE c VALUE 'N',

END OF t_frgroup.

My issue is, after adding those fields, my collect statement no longer works:

LOOP AT t_lips.

MOVE-CORRESPONDING t_lips TO t_frgroup.

COLLECT t_frgroup.

ENDLOOP.

I need it to collect with the key being mfrgr. How can I do this? After adding the fields the collect statement now acts as an insert (I assume that matnr is now acting as the key) instead of collect.

Regards,

Aaron

I had to make changes to some code and these changes required me to add some fields to an internal table. Below is what the table looked like before I made any changes:

DATA : BEGIN OF t_frgroup OCCURS 0,

  • BEGIN CHANGE 02/11/03

hazmat TYPE c,

  • END CHANGE 02/11/03

mfrgr LIKE lips-mfrgr,

brgew LIKE lips-brgew,

lfimg LIKE lips-lfimg,

qtypal LIKE w_nbr_palletsx,

qtypce LIKE w_nbr_palletsx,

vstel LIKE likp-vstel,

no_cnvrt TYPE c,

END OF t_frgroup.

This is what it looked like after I made the changes:

DATA : BEGIN OF t_frgroup OCCURS 0,

  • BEGIN CHANGE 02/11/03

hazmat TYPE c,

  • END CHANGE 02/11/03

mfrgr LIKE lips-mfrgr,

brgew LIKE lips-brgew,

lfimg LIKE lips-lfimg,

qtypal LIKE w_nbr_palletsx,

qtypce LIKE w_nbr_palletsx,

vstel LIKE likp-vstel,

no_cnvrt TYPE c,

matnr TYPE lips-matnr,

vbeln TYPE lips-vbeln,

posnr TYPE lips-posnr,

qty LIKE vblkp-lfimg,

vrkme LIKE lips-vrkme,

converted(1) TYPE c VALUE 'N',

END OF t_frgroup.

My issue is, after adding those fields, my collect statement no longer works:

LOOP AT t_lips.

MOVE-CORRESPONDING t_lips TO t_frgroup.

COLLECT t_frgroup.

ENDLOOP.

I need it to collect with the key being mfrgr. How can I do this? After adding the fields the collect statement now acts as an insert (I assume that matnr is now acting as the key) instead of collect.

Regards,

Aaron

9 REPLIES 9
Read only

Former Member
0 Likes
1,177

hi ,

check this ..

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,

venkat.

Read only

Former Member
0 Likes
1,177

Hi Davis,

COLLECT statement will work pefectly in your case , but that will not suit your requirement

you need to use AT END OF


LOOP AT t_lips.
   MOVE-CORRESPONDING t_lips TO t_frgroup.
  append t_frgroup.
ENDLOOP.

SORT T_FRGROUP BY MFRGR.

loop at t_frgroup.
  at end of  MFRGR.
     SUM.
   ENDAT.
ENDLOOP.  

Read only

0 Likes
1,177

Chandrasekhar, it doesn't work in my case and it did work, and suit my requirements, before I added fields to the table. This has been working since 2003.

venkat, thanks for that but how can I give the table a header line? If I can't give it a header line then I will have to make more changes than I want.

Regards,

Davis

Read only

0 Likes
1,177

Hi Davis,

Yes its true that it worked perfectly before

but now after adding some more fields it will not work the same way as it worked before

COLLECT will sum up the numeric fields when the character fields are same , so in this case the newly added field values may be different

Read only

0 Likes
1,177

Chandrasekhar, thanks for that explanation. So, because the material number is different it won't sum. OK, back to the drawing board.

Thanks!

Davis

Read only

Former Member
0 Likes
1,177

Hi Aaron,

1. Define the table keys while defining your internal table.

2. The order of the fields in the structure should be that the key fields come first , then the quantity fields and amount fields next.

3. Sort the table by the key fields before the loop.

The collect statment is creating news entries because If the system finds an entry with the key fields , the numeric fields that are not part of the table key are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead. Clearly the system is unable to find the existing entry because the key fields are not defined in your internal table or the fields are are out of order.

Hope this helps.

A simple example depicting this is as follows :


TYPES: BEGIN OF COMPANY, 
        NAME(20) TYPE C, 
        SALES    TYPE I, 
      END OF COMPANY. 

DATA: COMP    TYPE COMPANY, 
      COMPTAB TYPE HASHED TABLE OF COMPANY 
                                WITH UNIQUE KEY NAME. 

COMP-NAME = 'Duck'.  COMP-SALES = 10. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Duck'.  COMP-SALES = 30. COLLECT COMP INTO COMPTAB. 

regards,

Advait Gode.

Edited by: Advait Gode on Mar 28, 2008 3:50 PM

Read only

Former Member
0 Likes
1,177

Hello ,

The COLLECT will compare fields of the header of t_frgroup with corresponding fields in the internal table that are not of type P,I and F when trying to do a collect and then either insert a new line or add the PIF type fields to each other. Since you added at least one new C type field you have effectively modified the key of the table. You can avoid the problem by delcaring your internal table with an explicit unique key(mfrgr) as was suggested by an earlier post.

Regards

Greg Kern

Read only

0 Likes
1,177

Greg,

How can I declare the table, with a key, while preserving the header line?

Regards,

Davis

Read only

Former Member
0 Likes
1,177

*Collect statement 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* ...

In your case ..

(I need it to collect with the key being mfrgr. How can I do

this? After adding the fields the collect statement now acts as

an insert (I assume that matnr is now acting as the key)

instead of collect. )

I think MATNR is changing and a new record is being inserted

into the internal table ...