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

Collect statement - multiple fields

Former Member
0 Likes
10,813

Hi,

I have a requirement to do a collect on the OPBEL ABDAT BISDAT fields and then add the amount and is it possible to compare multiple fields to do the collect or else please suggest me any other logic.

As you see below OPBEL has the order number and ABDAT & BISDAT are DATE Intervals and my requirement is to compare the OPBEL ABDAT BISDAT and if all have the same value then collect IBTRG which is the amount.

BEFORE :-

OPBELABDATBISDATIBTRG
2000001087042012010120120926238.62
2000001087042012010120121026156.18
200000108704201201012012102638.74
2000001087082011082520111231115.9
200000108708201108252011123157.16
200000108708201108252011123125.32

AFTER :-

OPBELABDATBISDATIBTRG
2000001087042012010120120926238.62
2000001087042012010120121026194.92
2000001087082011082520111231198.38

Please help ..

Regards

Praneeth Kumar

10 REPLIES 10
Read only

FredericGirod
Active Contributor
0 Likes
4,534

Hi,

and ?  it doesn't work ?

regards

Fred

Read only

rosenberg_eitan
Active Contributor
0 Likes
4,534
Read only

Former Member
0 Likes
4,534

Well, the COLLECT statement should work.

Try:

LOOP AT t_table_before INTO w_values.

   COLLECT w_values INTO t_table_after.

ENDLOOP.

Read only

GirieshM
Active Contributor
0 Likes
4,534

Hi Praneeth,

Can you please check the BISDAT is a key field. If it is then due to the mismatch of date (20120926, 20121026) which is a key it considering it as various records.

Please find the analysis while using collect statement:

  • First      it will check in internal table for any record matching with the key in      work area data.
  • If it      couldn’t find any matching record, then the new data from work area will      be inserted in internal table
  • If any      record found with the same key, then instead of inserting a new record, it      will add the numeric field values of work area components with the      corresponding field components in the matched record and update the      internal table record.

Note : Source from http://braincybersolutions.com/sap-tutorials/abap/collect-statement-in-abap/

Read only

former_member209120
Active Contributor
0 Likes
4,534

Hi Praneeth Kumar,

You can try like this..

TYPES : BEGIN OF ty_final,
         opbel TYPE string,
         abdat TYPE abdat,
         bisdat TYPE bisdat,
         ibtrg TYPE p DECIMALS 2,
         END OF ty_final.

DATA : total TYPE p DECIMALS 2.


DEFINE final.
   wa_final-opbel  = &1.
   wa_final-abdat  = &2.
   wa_final-bisdat = &3.
   wa_final-ibtrg  = &4.
   append wa_final to it_final.
END-OF-DEFINITION.


DATA : it_final TYPE TABLE OF ty_final,
        it_final1 TYPE TABLE OF ty_final,
        wa_final TYPE ty_final.


final  '200000108704' '20120101'  '20120926'  '238.62'.
final  '200000108704' '20120101'  '20121026'  '156.18'.
final  '200000108704' '20120101'  '20121026'  '38.74'.
final  '200000108708' '20110825'  '20111231'  '115.9'.
final  '200000108708' '20110825'  '20111231'  '57.16'.
final  '200000108708' '20110825'  '20111231'  '25.32'.


LOOP AT it_final INTO wa_final.
   COLLECT wa_final INTO it_final1.
   CLEAR wa_final.
ENDLOOP.

LOOP AT it_final1 INTO wa_final.

   WRITE: / '|', wa_final-opbel,
            '|', wa_final-abdat,
            '|', wa_final-bisdat,
            '|', wa_final-ibtrg,'|'.
   ULINE.
ENDLOOP.


Regards,

Ramesh.T

Read only

former_member129652
Active Participant
0 Likes
4,534

Please provide your code, especially the declaration of the internal tables.

Read only

chundru_ravindra
Participant
0 Likes
4,534

This message was moderated.

Read only

Former Member
0 Likes
4,534

Hi Praneeth,

A working example -

Declare a structure like -

A table like

Loop at the table whose details you want to collect, in this case it is GT_BSID. Code as

See GT_BSID in debug as

and

see GT_SOLD_BAL (collected table) as

Now check what you have missed or not used properly. Revert in case still not clear.

BR.

Read only

Former Member
0 Likes
4,534

This message was moderated.

Read only

Former Member
0 Likes
4,534

Hi,

I think normal collect should work. There is nothing specific in your requirements.

Any way just check this code

*------------------------------

   TYPES : BEGIN OF ty_data,
        opbel TYPE c LENGTH 12,
        abdat TYPE abdat,
        bisdat TYPE bisdat,
        kbetr TYPE kbetr,
    END OF ty_data.

DATA : t_data TYPE STANDARD TABLE OF ty_data,
      t_data_collect TYPE STANDARD TABLE OF ty_data,
       x_data TYPE ty_data.

START-OF-SELECTION.




  x_data-opbel = '200000108704'.
  x_data-abdat = '20120101'.
  x_data-bisdat = '20120926'.
  x_data-kbetr  = '238.62'.
  APPEND x_data TO t_data.

  x_data-opbel = '200000108704'.
  x_data-abdat = '20120101'.
  x_data-bisdat = '20121026'.
  x_data-kbetr  = '156.18'.
  APPEND x_data TO t_data.
  x_data-opbel = '200000108704'.
  x_data-abdat = '20120101'.
  x_data-bisdat = '20121026'.
  x_data-kbetr  = '38.74'.
  APPEND x_data TO t_data.

  x_data-opbel = '200000108708'.
  x_data-abdat = '20110825'.
  x_data-bisdat = '20111131'.
  x_data-kbetr  = '115.9'.
  APPEND x_data TO t_data.
  x_data-opbel = '200000108708'.
  x_data-abdat = '20110825'.
  x_data-bisdat = '20111131'.
  x_data-kbetr  = '57.16'.
  APPEND x_data TO t_data.
  x_data-opbel = '200000108708'.
  x_data-abdat = '20110825'.
  x_data-bisdat = '20111131'.
  x_data-kbetr  = '25.32'.
  APPEND x_data TO t_data.
  LOOP AT t_data INTO x_data.
    WRITE 😕  x_data-opbel,
            x_data-abdat,
            x_data-bisdat,
            x_data-kbetr.
  ENDLOOP.
  LOOP AT t_data INTO x_data.
    COLLECT x_data INTO t_data_collect.
  ENDLOOP.
skip 2.
  WRITE 😕 'After collect'.

    LOOP AT t_data_collect INTO x_data.
    WRITE 😕  x_data-opbel,
            x_data-abdat,
            x_data-bisdat,
            x_data-kbetr.
  ENDLOOP.

output :

collect test

200000108704 01.01.2012 26.09.2012          238,62
200000108704 01.01.2012 26.10.2012          156,18
200000108704 01.01.2012 26.10.2012           38,74
200000108708 25.08.2011 31.11.2011          115,90
200000108708 25.08.2011 31.11.2011           57,16
200000108708 25.08.2011 31.11.2011           25,32


After collect
200000108704 01.01.2012 26.09.2012          238,62
200000108704 01.01.2012 26.10.2012          194,92
200000108708 25.08.2011 31.11.2011          198,38

Regards,

Priyaranjan