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: 

Problem in collect statement

Former Member
0 Kudos

Hi Experts,

I need to sum up some fields on the basis of ebeln and I have done it using collect statement

but i am facing problem in moving those fields to another itab. Please advice me

SELECT WERK STAT34 LOSMENGE  EBELN EBELP MATNR
            LMENGE01 LMENGE02 LMENGE03 LMENGE04 LMENGE05 LMENGE06
       FROM QALS INTO CORRESPONDING FIELDS OF TABLE IT_QALS
        FOR ALL ENTRIES IN IT_EKPO WHERE EBELN EQ IT_EKPO-EBELN
        AND EBELP EQ IT_EKPO-EBELP AND MATNR EQ IT_EKPO-MATNR
        AND WERK EQ IT_EKPO-WERKS.

LOOP AT IT_QALS.
  MOVE IT_QALS-MATNR TO TY_QALS-MATNR.
  MOVE IT_QALS-EBELP TO TY_QALS-EBELP.
  MOVE IT_QALS-EBELN TO TY_QALS-EBELN.
  MOVE IT_QALS-LOSMENGE TO TY_QALS-LOSMENGE.
   MOVE IT_QALS-LMENGE06 TO TY_QALS-LMENGE06.
  COLLECT TY_QALS.
ENDLOOP.

LOOP AT TY_QALS.
  READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                              MATNR = TY_QALS-MATNR
                              WERKS = TY_QALS-WERKS.
  MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.
  APPEND IT_EKPO.
ENDLOOP.

My out is :

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , 0.000

4500000151 , 3,614.000 , FUELOIL , 0.000

4500014413 , 2,225.000 , FUELOIL , 0.000

4500014413 , 0.00(menge), FUELOIL , 5,409.586

4500014413 , 0.00(menge), FUELOIL , 4,731.834

But I want my output as:

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , 5,409.586

4500000151 , 3,614.000 , FUELOIL , 4,731.834

4500014413 , 2,225.000 , FUELOIL , 0.000

Please advice.

Karthik

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
0 Kudos

Right now I can see you are missing WERKS field in TY_QALS table.


LOOP AT IT_QALS.
  ....   
  "here move also WERKS to IT_QALS otherwise the key is not full
   MOVE IT_QALS-WERKS TO TY_QALS-WERKS.
   COLLECT TY_QALS.
ENDLOOP.

Now with second loop


LOOP AT TY_QALS.
  READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                              MATNR = TY_QALS-MATNR
                              WERKS = TY_QALS-WERKS.
   if sy-subrc = 0.

Suppose TY_QALS entry is like this

ty_qals-ebeln , ty_qals-matnr , ty_qals-wekrs, ty_qals-losmenge ...

4500000150 , FUELOIL , SOME_WERKS_VALUE, 5,409.586 , ...

and corresponding record in IT_EKPO

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-werks, it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , SOME_WERKS_VALUE, 0.000

Now when we use


  "if correspoding record was found
  MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.

the record in IT_EKPO would look like

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-werks, it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , SOME_WERKS_VALUE, 5,409.586


"with modify we can change this record 
modify it_ekpo.

Try this and debug at least one entrie record step by step and if still not resolved please write what you get in each table on each step.

Regards

Marcin

10 REPLIES 10

MarcinPciak
Active Contributor
0 Kudos

Hi,

Try this:


LOOP AT TY_QALS.
  READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                              MATNR = TY_QALS-MATNR
                              WERKS = TY_QALS-WERKS.
 if sy-subrc = 0.  "entry must exists
    MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.
    MODIFY IT_EKPO.  "modify instead of append
endif.
ENDLOOP.

Regards

Marcin

0 Kudos

Hi Maric,

Modify it_ekpo is not even reading any single data as I am getting the output as 0.

Please advice

Karthik

0 Kudos

Hi Kathik,

If you read the table with


READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                              MATNR = TY_QALS-MATNR
                              WERKS = TY_QALS-WERKS.
 if sy-subrc = 0. 

and sy-subrc ne 0 this means there is no corresponding entry in IT_EKPO (you can check in debug). It will only modify your IT_EKPO entry if there is one with the key

EBELN = TY_QALS-EBELN

MATNR = TY_QALS-MATNR

WERKS = TY_QALS-WERKS.

Check the content of tables IT_EKPO and TY_QALS and compare which keys exists and which do not.

Regards

Marcin

0 Kudos

Hi Marcin,

I have checked in debugging, but the problem here is there is value in ty_qals-losmenge

but its not getting moved to it_ekpo. Please advice

LOOP AT TY_QALS.
    READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                                MATNR = TY_QALS-MATNR
                                WERKS = TY_QALS-WERKS.
    IF SY-SUBRC = 0.
      MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.
      modify IT_EKPO.
    ENDIF.
  ENDLOOP.

Karthik.

Former Member
0 Kudos

Hi !

first of all dont give loop at TY_QALS and give loop at it_ekpo

i think you are getting output lke that b'coz you are appending it_ekpo that's why it is appending in new line of it_ekpo instead of that either use collect or modify statement.

i think modify will be a good option.

Former Member
0 Kudos

This message was moderated.

faisal_altaf2
Active Contributor
0 Kudos

Hi,

Do the following Change Hope will Solve out your Problem,

LOOP AT ty_qals.
  READ TABLE it_ekpo WITH KEY ebeln = ty_qals-ebeln
                              matnr = ty_qals-matnr
                              werks = ty_qals-werks.
  IF sy-subrc EQ 0.
    MOVE ty_qals-losmenge TO it_ekpo-losmenge.
    COLLECT it_ekpo into it_ekpo2. " Here Define an other internal table and Collect in that like me Collecting in it_ekpo2
  ENDIF.

ENDLOOP.

Best Regards,

Faisal

Former Member
0 Kudos

Hi ,

sort the internal tables based on fileds before using collect.

Regards

MarcinPciak
Active Contributor
0 Kudos

Right now I can see you are missing WERKS field in TY_QALS table.


LOOP AT IT_QALS.
  ....   
  "here move also WERKS to IT_QALS otherwise the key is not full
   MOVE IT_QALS-WERKS TO TY_QALS-WERKS.
   COLLECT TY_QALS.
ENDLOOP.

Now with second loop


LOOP AT TY_QALS.
  READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                              MATNR = TY_QALS-MATNR
                              WERKS = TY_QALS-WERKS.
   if sy-subrc = 0.

Suppose TY_QALS entry is like this

ty_qals-ebeln , ty_qals-matnr , ty_qals-wekrs, ty_qals-losmenge ...

4500000150 , FUELOIL , SOME_WERKS_VALUE, 5,409.586 , ...

and corresponding record in IT_EKPO

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-werks, it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , SOME_WERKS_VALUE, 0.000

Now when we use


  "if correspoding record was found
  MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.

the record in IT_EKPO would look like

it_ekpo-ebeln , menge , it_ekpo-matnr , it_ekpo-werks, it_ekpo-losmenge

4500000150 , 3,006.000 , FUELOIL , SOME_WERKS_VALUE, 5,409.586


"with modify we can change this record 
modify it_ekpo.

Try this and debug at least one entrie record step by step and if still not resolved please write what you get in each table on each step.

Regards

Marcin

0 Kudos

Hi Marcin ,

Thanks a lot Marcin!!

Actually it was my mistake I did't move it_qals-werks to ty_qals-werks. But after moving when I tried the code below I got dump error, I don't know the reason :

LOOP AT TY_QALS.
    READ TABLE IT_EKPO WITH KEY EBELN = TY_QALS-EBELN
                                EBELP = TY_QALS-EBELP
                                MATNR = TY_QALS-MATNR
                                WERKS = TY_QALS-WERK.
    IF SY-SUBRC = 0.
      MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.
      modify IT_EKPO.
  ENDLOOP.

Instead of the above code I used the below and its working Marcin !!

LOOP AT IT_EKPO.
    READ TABLE TY_QALS WITH KEY EBELN = IT_EKPO-EBELN
                                EBELP = IT_EKPO-EBELP
                                MATNR = IT_EKPO-MATNR
                                WERK = IT_EKPO-WERKS.
    IF SY-SUBRC = 0.
      MOVE TY_QALS-LOSMENGE TO IT_EKPO-LOSMENGE.
      MODIFY IT_EKPO.
      CLEAR IT_EKPO.
    ENDIF.
  ENDLOOP.

Just changed little of my logic...Thanks to u, Marcin !!

Thanks

Have a great weekend )

Karthik