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 with collect statement

Bharath84
Participant
0 Likes
1,175

Hi All,

In my report for given company code and customer number, I have to get the values of field WRBTR(table BSID) for each product hierarchy based on sales document number. For each Billing document I have multiple product hierarchy and these product hierarchy may be duplicates also. In my output I am getting total of all duplicate product hierarchy. I want separate values even if the product hierarchy are duplicates. Below is my code. Please help me.

sort t_prod_details by vbeln prodh.

   delete adjacent duplicates from t_prod_details comparing vbeln prodh.

   loop at t_prod_details.

*     at new vbeln.

at new prodh.  

       clear: t_final.

       read table t_open_items with key vbeln = t_prod_details-vbeln.

       if sy-subrc ne 0.                "the open item is actual invoice

         read table t_open_items with key belnr = t_prod_details-vbeln.

         if sy-subrc eq 0.

           t_open_items-flag = 'X'.              

           modify t_open_items index sy-tabix.

         endif.

       else.

         t_open_items-flag = 'X'.         

         modify t_open_items index sy-tabix.

       endif.

      read table t_bseg_items with key belnr = t_open_items-belnr

                                       gjahr = t_open_items-gjahr

                                       bukrs = t_open_items-bukrs

                                       buzei = t_open_items-buzei.

      check sy-subrc eq 0.

*due date = base line date + discount days.

      perform calculate_due_date.

      if w_due_date lt p_ddate.      

        w_due_day = p_ddate - w_due_date .

        if p_days gt 0 and w_due_day gt p_days.

          continue.

        endif.

        move 'P' to t_final-over_due . "past due

        perform calculate_bucket.

      elseif w_due_date ge p_ddate.    "insert

        w_due_day = w_due_date - p_ddate.

        if p_days gt 0 and w_due_day gt p_days.

          continue.

        endif.

        move 'N' to t_final-over_due .

        perform calculate_bucket.

      endif.

      move t_open_items-bukrs to t_final-bukrs.

    endat.

    clear w_wrbtr.

    w_wrbtr =  t_open_items-wrbtr  .

    if t_open_items-shkzg eq 'H'.

      t_final-wrbtr = w_wrbtr * -1.

    else.

      move w_wrbtr to t_final-wrbtr.

    endif.

    if t_prod_details-prodh  eq space.

      move 'OTHERS' to t_final-prodh.

    else.

      move t_prod_details-prodh to t_final-prodh.

    endif.

    collect t_final.

    clear t_final.

  endloop.

Thanks,

Haritha

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,132

Hi Haritha,

Collect statement will add all the numeric data for the same keys.

If you want to have heirarchy level addition, then try to find atleat one key which would differentiate between two product heirarchies.

VBELN PRODH WRBTR

23         10         30

23         10         10

23          10        20

using collect u will get the result as

23         10         60

But if you want to collect for duplicate heirarchy, then find one key

23   10   X   30

23    10  Y  10

23    10  Z   20

So if you find one column which has different values then only you will be able to collect "wrbtr" for the duplicate  heirarchies.

6 REPLIES 6
Read only

Former Member
0 Likes
1,133

Hi Haritha,

Collect statement will add all the numeric data for the same keys.

If you want to have heirarchy level addition, then try to find atleat one key which would differentiate between two product heirarchies.

VBELN PRODH WRBTR

23         10         30

23         10         10

23          10        20

using collect u will get the result as

23         10         60

But if you want to collect for duplicate heirarchy, then find one key

23   10   X   30

23    10  Y  10

23    10  Z   20

So if you find one column which has different values then only you will be able to collect "wrbtr" for the duplicate  heirarchies.

Read only

Former Member
0 Likes
1,132

Hi Haritha

what is the structure of t_final.

All the fields right to wrbtr in t_final will be compared in COLLECT. See to that Prod Hierarchy is also right to WRBTR in t_final.

Read only

FredericGirod
Active Contributor
0 Likes
1,132

Hi,

what is the result of your code ?

what is the structure of t_prod_detail ?

I think your code have a problem with the AT NEW. If your table have the field : VBELN & PRODH

when you write at new PRODH, that will occurs each time VBELN or PRODH change. For example

VBELN   PRODH

1               10

1               20

2               20

your program will go in the AT NEW PRODH 3 times. the last time just because the VBELN change.

stop using the AT NEW.

regards

Fred

Read only

former_member209120
Active Contributor
0 Likes
1,132

Hi Haritha P

Try like this

if t_prod_details-prodh  eq space.

      move 'OTHERS' to t_final-prodh.

    else.

      move t_prod_details-prodh to t_final-prodh.

    endif.

    Sort t_final by vbeln.

    delete adjacent duplicates from t_final.

    collect t_final.

    clear t_final.

  endloop.

Read only

Bharath84
Participant
0 Likes
1,132

Hi All,

Below is the structure for t_final. This is 4.0B so occurs 0 was used.

data:begin of t_final occurs 0,

     over_due(1),

     bukrs like bsid-bukrs,

     kukla like kna1-kukla,

     xref1 like bseg-xref1,

     vkbur like vbak-vkbur,

     prodh like vbrp-prodh,

     kunnr like kna1-kunnr,

     reason(3),

     category(4),

     wrbtr like bsid-wrbtr,

     end of t_final.

data:begin of t_prod_details occurs 0,

     vbeln like vbrp-vbeln,

     posnr like vbrp-posnr,

     matnr like vbrp-matnr ,

     netwr like vbrp-netwr,

     prodh like vbrp-prodh,

     h_netwr like vbrk-netwr,

end of t_prod_details.

Here producdt hierarchy 109469220AUA102084 has total 47.693,13 which is a sum of values

1677,22

46.015,91.

But I want separately in the output

109469220AUA102084          1677,22

Totals                                  1677.22

109469220AUA102084        46.015,91

Totals                                46.015,91

How to get this one. Please help me.

Thanks,

Haritha

Read only

Former Member
0 Likes
1,132

Thats what I replied for...

See some key which is different for two same heirarchies, then only you would be able to get the desired result.

Your complete combination must be different for collect statement to get the desired result.