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: 

issue regarding collect statement

former_member477101
Participant
0 Kudos

HI,

     collect statement is used to compress unique data sets, and when it comes to amount fields it sums up the values,but  i have to bifurcate records in internal table  (i mean to  separate records ) while  using collect statement  , whenever two different amount values are present for same field.

am i supposed to use different data types or different statement for the filed so that it should not collect .                                                                                                                                                          

please check below screen shot for exact explanation.

Example :

code             work     unit      Quantity   Rate

200021      shuttering   m2     100           150

200021      shuttering   m2     100           120

200022      PCC          m3      90            200

200022      PCC          m3      90            200

since last two records contained same value it has been compressed , whereas  first two records contains same values except  rate value, so i needed to show output in the below manner ,how it can  be acheived.

Final output should be in this manner ..

200021      shuttering   m2     100           150

200021      shuttering   m2     100           120

200022      PCC          m3      180           400

Thanks & Regards

J.Prakash Rao

1 ACCEPTED SOLUTION

FredericGirod
Active Contributor
0 Kudos

Hi,

did you try to change the data type of the RATE field ? to set it in TYPE C ?

regards

Fred

16 REPLIES 16

FredericGirod
Active Contributor
0 Kudos

Hi,

did you try to change the data type of the RATE field ? to set it in TYPE C ?

regards

Fred

0 Kudos

HI Fredric,

                ya tried but its leading to dumps,  as unable to convert data types.

former_member212002
Active Contributor
0 Kudos

You want to club lines on the combination of Code,Work, Unit and Rate. For that you have to ensure that all these fields are characters.

Thanks

Abhinab

0 Kudos

Hi abinav,

                   does changing data types solve my prob, but its leading to dumps as it unable to convert amount fields into char.

Thanks & REgrds.

sriharsha_parayatham
Participant
0 Kudos

is your final output

200021      shuttering   m2     100           150

200021      shuttering   m2     100           120

200022      PCC          m3      180           400

or

200021      shuttering   m2     100           150

200021      shuttering   m2     100           120

200022      PCC          m3      180           200

Former Member
0 Kudos

Hi,

try this in filling the itab:

before append/collect in itab read itab with the all fields.

if sy-subrc <> 0 use append else use collect.

Hope it helps.

Regards, Dieter

Former Member
0 Kudos

Hi Prakash,

DATA: lf_index TYPE sy-tabix,

            lwa_final LIKE wa_final.

LOOP at i_final INTO wa_final.

*-Get the next record

lf_index =  sy-tabix + 1.

READ table I_final INTO lwa_final with iINDEX lf_index.

IF sy-subrc IS INITIAL AND wa_final-code         = lwa_final-code 

                                  AND  wa_final-work         =  lwa_final-work

                                  AND  wa_final-unit           =  lwa_final-unit

                                  AND  wa_final-Quantity    = lwa_final-Quantity

                                  AND  wa_final-Rate         =  lwa_final-Rate .

wa_final-rate =  wa_final-rate + lwa_final-rate .

*-Append the record into new internal table and disply it

APPEND wa_final TO it_final.

ENDIF.

ENDLOOP.
SORT the internal table.

This will solve your issue.

Cheers,

Pravin

asim_isik
Active Participant
0 Kudos

collect itab comparing by all fields.

Former Member
0 Kudos

Hi Prakash Rao,

Please paste the below code , it will work according to your criteria...

TYPES : BEGIN OF ty_emp,

    code  TYPE char20,

   work  TYPE char20,

   unit  TYPE char2,

   quantity   TYPE i,

   rate  TYPE char3,

   END OF ty_emp.

DATA : it_emp TYPE STANDARD TABLE OF ty_emp ,

        wa_emp TYPE ty_emp.

START-OF-SELECTION.

BREAK-POINT.

   wa_emp-code = '200021'.

   wa_emp-work = 'shuttering '.

   wa_emp-unit = 'm2'.

   wa_emp-quantity = '100'.

   wa_emp-rate = '150'.

   COLLECT wa_emp INTO it_emp.

   wa_emp-code = '200021'.

   wa_emp-work = 'shuttering '.

   wa_emp-unit = 'm2'.

   wa_emp-quantity = '100'.

   wa_emp-rate = '120'.

   COLLECT wa_emp INTO it_emp.

   wa_emp-code = '200022'.

   wa_emp-work = 'pcc '.

   wa_emp-unit = 'm3'.

   wa_emp-quantity = '90'.

   wa_emp-rate = '200'.

   COLLECT wa_emp INTO it_emp.

   wa_emp-code = '200022'.

   wa_emp-work = 'pcc '.

   wa_emp-unit = 'm3'.

   wa_emp-quantity = '90'.

   wa_emp-rate = '200'.

   COLLECT wa_emp INTO it_emp.

In the It_emp which will have...

200021shutteringm2100150
200021shutteringm2100120
200022pccm3180200

Thanks ,

Vijay SR

0 Kudos

Hi,

    hope the below logic works for you.

1) Create an 2 new internal ITAB2 & ITAB3, pass all the values from ITAB1 to ITAB2.

2) Sort ITAB2 by Code Work Unit Quantity Rate.

3) Delete adjacent duplicate from ITAB2 comparing all the fields.

4) Loop at ITAB2 into wa_itab2.

         Loop at ITAB1 into wa_itab1 where Code = wa_itab2-code

                                                      and Work = wa_itab2-work

                                                      and unit = wa_itab2-unit

                                                      and quantity = wa_itab2-quantity

                                                      and rate = wa_itab2-rate.

               wa_itab3-code = wa_itab1-code.

               wa_itab3-work = wa_itab1-work.

               wa_itab3-unit = wa_itab1-unit.

               wa_itab3-quantity = wa_itab3-quantity + wa_itab1-quantity.

               wa_itab3-rate = wa_itab3-quantity + wa_itab1-rate.

               clear wa_itab1.

        endloop.

        if not wa_itab3 is initial.

           append wa_itab3 to itab3.

           clear wa_itab3.

        endif

        clear wa_itab2.

    endloop.

raymond_giuseppi
Active Contributor
0 Kudos

You could either

  • with COLLECT statement
    • Define sum criteria fields as character and not numeric type
    • Define sum criteria fields as keys of a sorted standard/hashed internal table
  • without COLLECT statement
    • Code in a SORT/LOOP

Regards,

Raymond

Former Member
0 Kudos

Assume you have value in itab1, Create another inter table of same type.. itab2

& also create two work area wa1 and wa2 of same type.


LOOP AT itab1 INTO wa1.

         LOOP AT itab2 INTO wa2 WHERE cocode = wa1-cocode AND

                                                                     work = wa1-work AND

                                                                      unit  =  wa1-unit.

           wa2-quantity  =  wa2-quantity + wa1-quantity.

           wa2-rate  =  wa2-rate + wa1-rate.

           MODIFY itab2 FROM wa2.

         ENDLOOP.

         IF sy-subrc NE 0.

           APPEND wa1 TO itab2.

         ENDIF.


ENDLOOP.


Now check itab2 for the required o/p.

rosenberg_eitan
Active Contributor
0 Kudos

FORM test_07 .

   TYPES: BEGIN OF tp_sbook_1 .

   TYPES: carrid      TYPE sbook-carrid ,
          luggweight  TYPE sbook-luggweight ,
          luggweighc  TYPE string ,
          luggweighs  TYPE p DECIMALS 4 ,
          count       TYPE i .
   TYPES: END OF tp_sbook_1 .

   DATA: it_sbook TYPE TABLE OF sbook .

   SELECT * INTO TABLE it_sbook
   FROM sbook .

   FIELD-SYMBOLS:  <st_sbook> LIKE LINE OF it_sbook .

   DATA: it_sbook_1 TYPE TABLE OF tp_sbook_1 .
   DATA: st_sbook_1 LIKE LINE OF it_sbook_1 .

   LOOP AT it_sbook ASSIGNING <st_sbook> .

     MOVE-CORRESPONDING <st_sbook> TO st_sbook_1 .

     st_sbook_1-luggweighc = st_sbook_1-luggweighs = <st_sbook>-luggweight .

     st_sbook_1-count = 1 .

     COLLECT st_sbook_1 INTO it_sbook_1 .

   ENDLOOP .

   FIELD-SYMBOLS: <st_sbook_1> LIKE LINE OF it_sbook_1 .

   LOOP AT it_sbook_1 ASSIGNING <st_sbook_1> .
     <st_sbook_1>-luggweight = <st_sbook_1>-luggweight / <st_sbook_1>-count .
   ENDLOOP.

   BREAK-POINT .

ENDFORM.


look at   it_sbook_1 .


regards.                                      

Former Member
0 Kudos

Hi Prakash,

               Please check out the below code and execute it.

TYPES: BEGIN OF TY_CODE,

        CODE TYPE C LENGTH 22,

        WORK TYPE C LENGTH 22,

        UNIT(2) TYPE C,

        QUAN TYPE I,

        RATE TYPE I,

      END OF TY_CODE.

DATA : WA_FINAL1 TYPE TY_CODE,

       IT_FINAL1 TYPE TABLE OF TY_CODE,

       IT_FINAL2 LIKE IT_FINAL1,

       WA_FINAL2 LIKE WA_FINAL1,

       IT_FINAL3 LIKE IT_FINAL1,

       WA_FINAL3 LIKE WA_FINAL1.

WA_FINAL1-CODE = '200021'.

WA_FINAL1-WORK = 'SHUTTERING'.

WA_FINAL1-UNIT = 'M2'.

WA_FINAL1-QUAN = '100'.

WA_FINAL1-RATE =  '150'.

APPEND WA_FINAL1 TO IT_FINAL1.

CLEAR WA_FINAL1.

WA_FINAL1-CODE = '200021'.

WA_FINAL1-WORK = 'SHUTTERING'.

WA_FINAL1-UNIT = 'M2'.

WA_FINAL1-QUAN = '100'.

WA_FINAL1-RATE =  '120'.

APPEND WA_FINAL1 TO IT_FINAL1.

CLEAR WA_FINAL1.

WA_FINAL1-CODE = '200022'.

WA_FINAL1-WORK = 'PCC'.

WA_FINAL1-UNIT = 'M3'.

WA_FINAL1-QUAN = '90'.

WA_FINAL1-RATE =  '200'.

APPEND WA_FINAL1 TO IT_FINAL1.

CLEAR WA_FINAL1.

WA_FINAL1-CODE = '200022'.

WA_FINAL1-WORK = 'PCC'.

WA_FINAL1-UNIT = 'M3'.

WA_FINAL1-QUAN = '90'.

WA_FINAL1-RATE =  '200'.

APPEND WA_FINAL1 TO IT_FINAL1.

CLEAR WA_FINAL1.

LOOP AT IT_FINAL1 INTO WA_FINAL1.

MOVE-CORRESPONDING WA_FINAL1 TO WA_FINAL2.

APPEND WA_FINAL2 TO IT_FINAL2.

ENDLOOP.

SORT IT_FINAL2 BY CODE WORK UNIT QUAN RATE.

DELETE ADJACENT DUPLICATES FROM IT_FINAL2 COMPARING ALL FIELDS.

LOOP AT IT_FINAL2 INTO WA_FINAL2.

    LOOP AT IT_FINAL1 INTO WA_FINAL1 WHERE CODE = WA_FINAL2-CODE

                                                                  AND WORK = WA_FINAL2-WORK              

                                                                  AND UNIT = WA_FINAL2-UNIT

                                                                  AND QUAN = WA_FINAL2-QUAN

                                                                  AND RATE = WA_FINAL2-RATE.

      WA_FINAL3-CODE  = WA_FINAL1-CODE.

      WA_FINAL3-WORK = WA_FINAL1-WORK.

      WA_FINAL3-UNIT    = WA_FINAL1-UNIT.

      WA_FINAL3-QUAN  = WA_FINAL3-QUAN + WA_FINAL1-QUAN.

      WA_FINAL3-RATE   = WA_FINAL3-RATE + WA_FINAL1-RATE.

      CLEAR WA_FINAL1.

    ENDLOOP.

    IF NOT WA_FINAL3 IS INITIAL.

       APPEND WA_FINAL3 TO IT_FINAL3.

       CLEAR WA_FINAL3.

    ENDIF.

    CLEAR WA_FINAL2.

ENDLOOP.

LOOP AT IT_FINAL3 INTO WA_FINAL3.

WRITE : / WA_FINAL3-CODE,WA_FINAL3-WORK,WA_FINAL3-UNIT,

                WA_FINAL3-QUAN,WA_FINAL3-RATE.

ENDLOOP.

Regards,

Lavanya

Former Member
0 Kudos

Hi,

The collect statement sums up all numeric fields based on the character-type key fields.

BR,

Nathan

0 Kudos

You can use the below code. I checked and got the output as per your requirement.

read table itab into wa index 1.

   loop at itab into wa1 from 2.

          if wa1 <> wa.

             append wa to it_final.

          else.

             wa-quantity = wa-quantity + wa-quantity.

             wa-rate     = wa-rate     + wa-rate.    "Probably rate will be constant If required do it

             append wa to it_final.

          endif.

     wa = wa1.

   endloop.


Thanks,

Venkadesh