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

Issue while using Collect statement in program

Former Member
0 Likes
1,914

Hi friend's,

i have some issue regarding Collect statement..

Issue is somthing like this,i have created a structure and internal table and work area with same type,

u can look in below code..i wann to collect 'enmng' f'ield value whose matnr,charg,mvt type ,aufnr,rsnum are same but rspos and lgort are differnt for this i need to collect 'ENMNG' field value can any one tel me what to do...it not collectng value instead of this it append entire row...

types: begin of ty_resb,

        rsnum   type resb-rsnum,                "Resrvation Number

        rspos   type resb-rspos,                "item position

        xloek   type resb-xloek,                "Item Deleted  if = 'X'

        matnr   type resb-matnr,                "Material Number

        lgort   type resb-lgort,                "Storage location  'REJT'

        charg   type resb-charg,                "Batch No

        enmng   type resb-enmng,                "Quantity withdrawn should be greater then '0'

        enwrt   type resb-enwrt,                "Value Withdrawn

        aufnr   type resb-aufnr,                "Order

        bwart   type resb-bwart,                "Movement type

   end of ty_resb.

data:tt_resb  type table of  ty_resb ,

      tt_resb1 type table of ty_resb ,

      tt_resb2 type standard table of ty_resb ,

**workarea.

      ts_resb type ty_resb,

      ts_resb2 type ty_resb,

      ts_resb1 type ty_resb.

**move data to another internal table

tt_resb1[] = tt_resb[].

sort tt_resb  by rsnum rspos matnr charg bwart lgort aufnr .

sort tt_resb1 by rsnum rspos matnr charg bwart lgort aufnr.

DELETE adjacent duplicates from tt_resb  comparing rsnum rspos matnr charg bwart lgort aufnr .

DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.

loop at tt_resb into ts_resb .

clear:ts_resb1,ts_resb2.

read table tt_resb1 into ts_resb1 with key rsnum = ts_resb-rsnum rspos = ts_resb-rspos matnr = ts_resb-matnr

                                            charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.

if ts_resb1-bwart = '261' .

    move ts_resb1-aufnr to ts_resb2-aufnr.

    move ts_resb1-matnr to ts_resb2-matnr.

    move ts_resb1-charg to ts_resb2-charg.

    move ts_resb1-bwart to ts_resb2-bwart.

    move ts_resb1-enwrt to ts_resb2-enwrt.

    move ts_resb1-lgort to ts_resb2-lgort.

    move ts_resb1-enmng to ts_resb2-enmng.

    collect ts_resb2 into tt_resb2.

endif.

clear ts_resb.

endloop.

Regard's,

shaikh khalid.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,877

Hi Shaikh,

I have added new declarations as highlighted below and new lines within your loop:

Execute your program in debug mode and see what happens to the internal tt_collect I added. The collect will sum the fields enmng for all entries that  have same value on the fields matnr,charg,mvt type ,aufnr,rsnum.

Question from me: what do you want to do with the collected/Summed up entries?

types: begin of ty_resb,

        rsnum   type resb-rsnum,                "Resrvation Number

        rspos   type resb-rspos,                "item position

        xloek   type resb-xloek,                "Item Deleted  if = 'X'

        matnr   type resb-matnr,                "Material Number

        lgort   type resb-lgort,                "Storage location  'REJT'

        charg   type resb-charg,                "Batch No

        enmng   type resb-enmng,                "Quantity withdrawn should be greater then '0'

        enwrt   type resb-enwrt,                "Value Withdrawn

        aufnr   type resb-aufnr,                "Order

        bwart   type resb-bwart,                "Movement type

   end of ty_resb.

data:tt_resb  type table of  ty_resb ,

      tt_resb1 type table of ty_resb ,

      tt_resb2 type standard table of ty_resb ,

types: begin of ty_collect,

  rsnum   type resb-rsnum,

  matnr   type resb-matnr,

  charg   type resb-charg,

  bwart   type resb-bwart,

  aufnr   type resb-aufnr, 

  enmng   type resb-enmng,

       end of ty_collect.

data: tt_collect type table of ty_collect,

      ts_collect type ty_collect.

**workarea.

      ts_resb type ty_resb,

      ts_resb2 type ty_resb,

      ts_resb1 type ty_resb.

**move data to another internal table

tt_resb1[] = tt_resb[].

sort tt_resb  by rsnum rspos matnr charg bwart lgort aufnr .

sort tt_resb1 by rsnum rspos matnr charg bwart lgort aufnr.

DELETE adjacent duplicates from tt_resb  comparing rsnum rspos matnr charg bwart lgort aufnr .

DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.

loop at tt_resb into ts_resb .

clear:ts_resb1,ts_resb2.

read table tt_resb1 into ts_resb1 with key rsnum = ts_resb-rsnum rspos = ts_resb-rspos matnr = ts_resb-matnr

                                            charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.

if ts_resb1-bwart = '261' .

    move ts_resb1-aufnr to ts_resb2-aufnr.

    move ts_resb1-matnr to ts_resb2-matnr.

    move ts_resb1-charg to ts_resb2-charg.

    move ts_resb1-bwart to ts_resb2-bwart.

    move ts_resb1-enwrt to ts_resb2-enwrt.

    move ts_resb1-lgort to ts_resb2-lgort.

    move ts_resb1-enmng to ts_resb2-enmng.

    collect ts_resb2 into tt_resb2.

   

     move-corresponding ts_resb1 to ts_collect.

     collect ts_collect into tt_collect

endif.

clear ts_resb.

clear ts_collect.

endloop.

12 REPLIES 12
Read only

Former Member
0 Likes
1,877

press F1 on the collect statement and read it's function. Collect is dependent on data types and the key of the internal table itself. So either alter your code to incorporate the correct prerequisities or stop using collect at all and SUM the fields at your own.

Read only

0 Likes
1,877

hi,

have u gone through my code will you tel me what exactly i need to do..as you r teling that do sum for that particular field ..after what i need to do shall i append taht internal table or modify...

how can i do that i am moving ts_resb1 data to ts_resb2 and collecting tt_resb2 ..

Read only

0 Likes
1,877

Hi,

put fields enmng and enwrt to the end of structure  ty_resb. It should work then. Numeric fields to collect have to be set to the end.

Regards,

Klaus

Read only

0 Likes
1,877

hi klaus,

as per you told ,

i put both field at end in ty_resb but still not working...

Read only

0 Likes
1,877

Hi,

all other fields than enmng and enwrt are key fields. Do you have duplicates in your table where aall key fields have identical content? On which fields do you want to have the sum values collected? May be you need less key fields in your table to get the right results.

You can give it a try with table fields

MATNR             <= This one as key field, the other fields should be collected

ENMNG

ENWRT

If this works fine, you can build your correct table ...

Regards,

Klaus

Read only

0 Likes
1,877

hi,...

do not move ts_resb1 to ts_resb2 .

just collect ts_resb1 into internal table .

alternate solution:.....

you can do one thing if collect is not working for any reason.:....

loop at tt_resb into ts_resb  .

  

at new of aufnr .(assuming it top to be left most)

   endat,

at end of aufnr .

ts_resb-tt_resb2.

if ts_resb1-bwart = '261' .

acumulate the sum in the variable and assign to workarea and append to internal table.

endat

endloop.

Read only

0 Likes
1,877

Hi,

Kindly have a look of my structure....

rsnum and rspos is my key field

befor loop and read statmnt i deleted duplictate record

types: begin of ty_resb,

        rsnum   type resb-rsnum,                "Resrvation Number

        rspos   type resb-rspos,                "item position

        xloek   type resb-xloek,                "Item Deleted  if = 'X'

        matnr   type resb-matnr,                "Material Number

        lgort   type resb-lgort,                "Storage location  'REJT'

        charg   type resb-charg,                "Batch N

        aufnr   type resb-aufnr,                "Order

        bwart   type resb-bwart,                "Movement type

        enmng   type resb-enmng,                "Quantity withdrawn should be greater then '0'

        enwrt   type resb-enwrt,                "Value Withdrawn


   end of ty_resb.

sort tt_resb  by rsnum rspos matnr charg bwart lgort aufnr .

  tt_resb1[] = tt_resb[].

DELETE adjacent duplicates from tt_resb  comparing rsnum rspos matnr charg bwart lgort aufnr .

DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.

break abap1.

loop at tt_resb into ts_resb .

clear:ts_resb1,ts_resb2.

read table tt_resb1 into ts_resb1 with key rsnum = ts_resbrsnum rspos = ts_resb-rspos matnr = ts_resb-matnr charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.

if ts_resb1-bwart = '261' .

   move ts_resb1-aufnr to ts_resb2-aufnr.

   move ts_resb1-matnr to ts_resb2-matnr.

   move ts_resb1-charg to ts_resb2-charg.

   move ts_resb1-bwart to ts_resb2-bwart.

   move ts_resb1-lgort to ts_resb2-lgort.

    move ts_resb1-enwrt to ts_resb2-enwrt.

   move ts_resb1-enmng to ts_resb2-enmng .

   collect ts_resb2 into tt_resb2.



while rerading data based on key field rsnum rspos along with non-key firld matnr charg aufnr ....


what to do other then this?i am not able to get an idea..


regard's,

shaikh khalid.

Read only

Former Member
0 Likes
1,877

Hi ShaiKh,

i Hope this information will help you.

Collect work on Data type integer (i).

if the fields Value which are of Data type c or n must be same then Collect will work.

if the fields Value which are of Data type c or n are different then Append will work.

So, Please the value in the internal table Once.

Read only

Former Member
0 Likes
1,878

Hi Shaikh,

I have added new declarations as highlighted below and new lines within your loop:

Execute your program in debug mode and see what happens to the internal tt_collect I added. The collect will sum the fields enmng for all entries that  have same value on the fields matnr,charg,mvt type ,aufnr,rsnum.

Question from me: what do you want to do with the collected/Summed up entries?

types: begin of ty_resb,

        rsnum   type resb-rsnum,                "Resrvation Number

        rspos   type resb-rspos,                "item position

        xloek   type resb-xloek,                "Item Deleted  if = 'X'

        matnr   type resb-matnr,                "Material Number

        lgort   type resb-lgort,                "Storage location  'REJT'

        charg   type resb-charg,                "Batch No

        enmng   type resb-enmng,                "Quantity withdrawn should be greater then '0'

        enwrt   type resb-enwrt,                "Value Withdrawn

        aufnr   type resb-aufnr,                "Order

        bwart   type resb-bwart,                "Movement type

   end of ty_resb.

data:tt_resb  type table of  ty_resb ,

      tt_resb1 type table of ty_resb ,

      tt_resb2 type standard table of ty_resb ,

types: begin of ty_collect,

  rsnum   type resb-rsnum,

  matnr   type resb-matnr,

  charg   type resb-charg,

  bwart   type resb-bwart,

  aufnr   type resb-aufnr, 

  enmng   type resb-enmng,

       end of ty_collect.

data: tt_collect type table of ty_collect,

      ts_collect type ty_collect.

**workarea.

      ts_resb type ty_resb,

      ts_resb2 type ty_resb,

      ts_resb1 type ty_resb.

**move data to another internal table

tt_resb1[] = tt_resb[].

sort tt_resb  by rsnum rspos matnr charg bwart lgort aufnr .

sort tt_resb1 by rsnum rspos matnr charg bwart lgort aufnr.

DELETE adjacent duplicates from tt_resb  comparing rsnum rspos matnr charg bwart lgort aufnr .

DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.

loop at tt_resb into ts_resb .

clear:ts_resb1,ts_resb2.

read table tt_resb1 into ts_resb1 with key rsnum = ts_resb-rsnum rspos = ts_resb-rspos matnr = ts_resb-matnr

                                            charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.

if ts_resb1-bwart = '261' .

    move ts_resb1-aufnr to ts_resb2-aufnr.

    move ts_resb1-matnr to ts_resb2-matnr.

    move ts_resb1-charg to ts_resb2-charg.

    move ts_resb1-bwart to ts_resb2-bwart.

    move ts_resb1-enwrt to ts_resb2-enwrt.

    move ts_resb1-lgort to ts_resb2-lgort.

    move ts_resb1-enmng to ts_resb2-enmng.

    collect ts_resb2 into tt_resb2.

   

     move-corresponding ts_resb1 to ts_collect.

     collect ts_collect into tt_collect

endif.

clear ts_resb.

clear ts_collect.

endloop.

Read only

0 Likes
1,877

Hi John,

yes your logic is working brother thank you....

Thread was closed...

Regard's,

shaikh Ali.

Read only

0 Likes
1,877

Hi Shaikh,

I am glad we helped you.

Please mark the thread closed and assign some points too.

Thank you,

John

Read only

Former Member
0 Likes
1,877

Hi Friend's,

by declaring one more structure ty_collect with key field  and under loop and endloop  use move-corresponding statement to move ts_resb1 data to ts_collect .

after this use collect statement:  collect ts_collect into tt_collect .

Thread was closed....

Regard's,

shaikh Ali.