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: 

how to use collect statement

Former Member
0 Kudos

hi everybody,

how to use collect statement to get the total amount paid to different vendor payments

data : begin of wa occurs 0,
        bukrs type bsak-bukrs,
        lifnr type bsak-lifnr,
        land1 type lfa1-land1,
        name1 like lfa1-name1,
        dmbtr like bsak-dmbtr,
        count type i value 0,
        tot_vend  type i,
        vend type i.
data :end of wa.

data : itab like table of wa.

  select distinct bukrs lifnr waers from bsak into
corresponding fields of wa
          where bukrs in s_bukrs
          and   lifnr in s_lifnr
          and   bschl in s_bschl.

i want the total amount paid according to vendor i am  using this way but i am not getting


  loop at itab into wa.

wa-dmbtr = bsak-dmbtr.
collect wa-dmbtr into itab.

modify itab from wa transporting dmbtr.

i am unalbe to get it

can anybody help me regarding this if possible with example.

thanks in advance,

regards,

venu.

1 ACCEPTED SOLUTION

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi Venu,

types: BEGIN OF ty,

NAME(20),

SALES TYPE I,

END OF ty.

data : itab type standard table of ty,

itab1 type standard table of ty,

wa type ty,

wa1 type ty.

wa-NAME = 'Duck'. wa-SALES = 10.

append wa to itab.

wa-NAME = 'Tiger'. wa-SALES = 20.

append wa to itab.

wa-NAME = 'Duck'. wa-SALES = 30.

append wa to itab.

loop at itab into wa.

wa1 = wa.

collect wa1 into itab1.

endloop.

loop at itab1 into wa1.

write : / wa1-name , wa1-sales.

endloop.

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

<b>If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.</b>

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

In the program you mentioned yesterday,I am not able to get the logic since many lines are commented.

4 REPLIES 4

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

This will collect the numeric values by vendor.

* Declare an internal table with just vendor number
* and the values.
data : begin of tot occurs 0,
        lifnr type bsak-lifnr,
        tot_vend  type i,
        vend type i.
data :end of tot.
* Loop at your internal table and move those fields to the
* totals internal table and collect.
  loop at wa.
    move-corresponding wa to tot.
    collect tot.
  endloop.

Regards,

Rich Heilman

former_member214131
Active Contributor
0 Kudos

Hello,

I think you need to consider the debit/credit indcator, Document Type - Fields SHKZG, BLART from the table before you make a COLLECT.

Hope this helps you.

Best Regards, Murugesh AS

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi Venu,

types: BEGIN OF ty,

NAME(20),

SALES TYPE I,

END OF ty.

data : itab type standard table of ty,

itab1 type standard table of ty,

wa type ty,

wa1 type ty.

wa-NAME = 'Duck'. wa-SALES = 10.

append wa to itab.

wa-NAME = 'Tiger'. wa-SALES = 20.

append wa to itab.

wa-NAME = 'Duck'. wa-SALES = 30.

append wa to itab.

loop at itab into wa.

wa1 = wa.

collect wa1 into itab1.

endloop.

loop at itab1 into wa1.

write : / wa1-name , wa1-sales.

endloop.

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

<b>If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.</b>

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

In the program you mentioned yesterday,I am not able to get the logic since many lines are commented.

Former Member
0 Kudos

Hi,

See this sample code

TYPES: BEGIN OF ty_sap,

gl_acct LIKE zcs_sap_fields-gl_acct,

wbs_element LIKE zcs_sap_fields-wbs_element,

cost_center LIKE zcs_sap_fields-cost_center,

status LIKE zcs_sap_fields-status,

count TYPE z_counter,

amount LIKE zcs_sap_fields-amount,

user LIKE zcs_variant-sap_user,

manager LIKE zcs_variant-mf_group,

END OF ty_sap.

i_sap TYPE STANDARD TABLE OF ty_sap,

w_sap_collect TYPE ty_sap,

LOOP AT i_sap INTO w_sap.

CLEAR w_total.

w_total-key1 = c_sap.

w_total-key2 = w_sap-gl_acct.

w_total-key3 = w_sap-wbs_element.

w_total-key4 = w_sap-cost_center.

w_total-count = w_sap-count.

w_total-amount = w_sap-amount.

COLLECT w_total INTO i_total.

ENDLOOP.

No need to modify after collect. Collect itself will modify the itab.

U can further use another collect too this will append one mre line to the i_total.

Hope this helps.

Thanks & Regards,

Judith.