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: 

Collect statement not working properly

Former Member
0 Kudos

hi all,

in the below code i used collect statement to collect data from 3 differnet internal tables and then display in one final table.

but in the output i am getting wrong values.


 6609001
               10,000.00           23,819.04         23,819.04          2,601.00    " last 3 coloums are wrong values
 A5400   57,495.36           95,276.16         95,276.16         10,404.00    " last 3 coloums are wrong values
 28         67,495.36          119,095.20        119,095.20         13,005.00    " last 3 coloums are wrong values

here is the code


REPORT  ZTEMP13 no standard page heading LINE-SIZE 300.
  perform actual.
  CLEAR WA_BSEG.
  perform sply.
  CLEAR WA_BSEG1.
  perform lyac.
  CLEAR WA_BSEG2.
  perform TACT.
  CLEAR WA_BSEG3.
  perform pri_actual.
  perform final.
*&---------------------------------------------------------------------*
*&      Form  ACTUAL
FORM ACTUAL .
  SELECT hkont
  belnr
  gjahr
  KOSTL
  AUFNR
  BUKRS
  PSWBT
  WRBTR FROM BSEG
  INTO TABLE IT_BSEG
  WHERE GJAHR EQ S_GJAHR
  AND HKONT IN S_HKONT
  and kostl in s_kostl.
  IF not it_bseg[] IS INITIAL.
    SELECT belnr
    GJAHR
    MONAT
    BUKRS
    BUDAT FROM BKPF
    INTO TABLE IT_BKPF
    FOR ALL ENTRIES IN IT_BSEG
    WHERE MONAT eq s_monat
          AND belnr = it_BSEG-belnr
          AND GJAHR = it_BSEG-GJAHR
          AND BUKRS = it_BSEG-BUKRS.
  ENDIF.
ENDFORM.                    " ACTUAL
*&---------------------------------------------------------------------*
*&      Form  PRI_ACTUAL
FORM PRI_ACTUAL .
  sort it_bseg by hkont aufnr kostl pswbt.
  LOOP AT IT_BSEG INTO WA_BSEG.
    wa_coll-hkont = wa_bseg-hkont.
    wa_coll-aufnr = wa_bseg-aufnr.
    wa_coll-kostl = wa_bseg-kostl.
    wa_coll-pswbt = wa_bseg-pswbt.
    read table IT_BSEG1 INTO WA_BSEG1 with key hkont = wa_coll-hkont.
    wa_coll-hkont1 = wa_bseg1-hkont.
    wa_coll-aufnr1 = wa_bseg1-aufnr.
    wa_coll-kostl1 = wa_bseg1-kostl.
    wa_coll-pswbt1 = wa_bseg1-pswbt.
    read table IT_BSEG2 INTO WA_BSEG2 with key hkont = wa_coll-hkont.
    wa_coll-hkont2 = wa_bseg2-hkont.
    wa_coll-aufnr2 = wa_bseg2-aufnr.
    wa_coll-kostl2 = wa_bseg2-kostl.
    wa_coll-pswbt2 = wa_bseg2-pswbt.
    read table IT_BSEG3 INTO WA_BSEG3 with key hkont = wa_coll-hkont.
    wa_coll-hkont3 = wa_bseg3-hkont.
    wa_coll-aufnr3 = wa_bseg3-aufnr.
    wa_coll-kostl3 = wa_bseg3-kostl.
    wa_coll-pswbt3 = wa_bseg3-pswbt.
    collect wa_coll into it_coll.
    clear : wa_coll,wa_bseg3,wa_bseg2,wa_bseg1.
  endloop.
ENDFORM.                    " PRI_ACTUAL
*&---------------------------------------------------------------------*
*&      Form  SPLY
*&---------------------------------------------------------------------*
FORM SPLY .
  S_GJAHR1 = S_GJAHR - 1.
  SELECT hkont
  belnr
  gjahr
  KOSTL
  AUFNR
  BUKRS
  PSWBT
  WRBTR FROM BSEG
  INTO TABLE IT_BSEG1
  WHERE GJAHR EQ S_GJAHR1
  AND HKONT IN S_HKONT
  and kostl in s_kostl.
  IF not it_bseg1[] IS INITIAL.
    SELECT belnr
    GJAHR
    MONAT
    BUKRS
    BUDAT FROM BKPF
    INTO TABLE IT_BKPF1
    FOR ALL ENTRIES IN IT_BSEG1
    WHERE MONAT eq s_monat
          AND belnr = it_BSEG1-belnr
          AND GJAHR = it_BSEG1-GJAHR
          AND BUKRS = it_BSEG1-BUKRS.
  endif.
  clear s_gjahr1.
ENDFORM.                    " SPLY
*&---------------------------------------------------------------------*
*&      Form  LYAC
FORM LYAC .

  S_GJAHR1 = S_GJAHR - 1.

  SELECT gjahr
         hkont
         KOSTL
         AUFNR
         BUKRS
         PSWBT
         belnr
         WRBTR FROM BSEG
         INTO TABLE IT_BSEG2
         WHERE GJAHR EQ S_GJAHR1
         AND   HKONT IN S_HKONT
         and   kostl in s_kostl.
  clear s_gjahr1.
ENDFORM.                    " LYAC
*&---------------------------------------------------------------------*
*&      Form  TACT
FORM TACT .
  SELECT hkont
         belnr
         gjahr
         KOSTL
         AUFNR
         BUKRS
         PSWBT
         WRBTR FROM BSEG
         INTO TABLE IT_BSEG3
         WHERE GJAHR EQ S_GJAHR
         AND HKONT IN S_HKONT
         and kostl in s_kostl.

  IF not it_bseg3[] IS INITIAL.
    SELECT belnr
           GJAHR
           MONAT
           BUKRS
           BUDAT FROM BKPF
           INTO TABLE IT_BKPF3
           FOR ALL ENTRIES IN IT_BSEG3
           WHERE MONAT gt 0 and monat le s_monat
           AND belnr = it_BSEG3-belnr
           AND GJAHR = it_BSEG3-GJAHR
           AND BUKRS = it_BSEG3-BUKRS.
  ENDIF.
ENDFORM.                    " TACT

Moderator message : Duplicate post locked. Continue with Original thread : [collect statement not working properly |;

Edited by: Vinod Kumar on Sep 16, 2011 1:38 PM

6 REPLIES 6

Former Member
0 Kudos


*&---------------------------------------------------------------------*
*&      Form  FINAL
FORM FINAL .
  data: lv_index type sy-tabix.
  sort it_coll by HKONT kostl aufnr.
  loop at it_coll into wa_coll.
    lv_index = sy-tabix.
    at new hkont.
      NEW-PAGE.
      write:1 sy-uline(90).
      write:/1 sy-vline(1),
             2 wa_coll-HKONT COLOR 3.
    endat.
    at new aufnr.
      sum.
      write:90 sy-vline(1).
      write:/1 sy-uline(90),
            /1 sy-vline(1),
             2 wa_coll-aufnr color 1,
            10 wa_coll-pswbt,
            30 wa_coll-pswbt1,
            48 wa_coll-pswbt2,
            66 wa_coll-pswbt3,
            90 sy-vline(1).
    endat.
    AT END OF hkont.
      read table it_coll into wa_coll index lv_index.
      write:/1 sy-uline(90),
            /1 sy-vline(1),
             2 wa_coll-kostl,
            90 sy-vline(1).
    endat.
    at end of hkont.
      sum.
      write:10 wa_coll-pswbt.
      write:30 wa_coll-pswbt1.
      write:48 wa_coll-pswbt2.
      write:66 wa_coll-pswbt3.
      write:/1 sy-uline(90).
    endat.
    CLEAR WA_COLL.
  endloop.
ENDFORM.                    " FINAL

Kindly please suggest me,

thanks in advance,

ben.

0 Kudos

Hi,

Have you declared all non numeric fields as a key fields for hashed table where you are collecting the values?

e.g. if you want to collect values in field c and table having fields a b c d then a,b and d has to be key fields of hashed tables so for primary key of a,b and d values will be collected in C.

i.e. suppose internal table has values a = name1, b = name2 and d = name3 C = 100

a = name1, b = name2 and d = name3 C = 10

a = x1 , b = name2 and d = name3 C = 10

a = name1, b = X2 and d = X3 C = 10

a = name1, b = name2 and d = name3 C = 20

Final output in hashed table would be

name1 name2 130 name3

X1 name2 10 name3

name1 X2 10 X3

Hope it is clear to you.

0 Kudos

sorry but i did not understand , can yo uplease be more specific.

0 Kudos

Can you please be more specific?

Former Member
0 Kudos

The design of your internal table is important for COLLECT statement to work correctly. Few things to note-

1. All fields that are of 'Character Nature' automatically become the key of internal table, i.e. against their unique combination the numeric values get added, there by leaving a single line in the internal table against a unique combination of key fields.

2. All fields of type C(CHAR) and N(NUMC) are treated as character fields.

3. In the internal table declaration, these fields must appear from the begining. After all character natured fields, all numeric fields should be declared.

Example-

Begin of it_tab occurs 0,

field1(4) type c,

field2(2) type n,

field3(6) type c,

field4(4) type n,

menge like mseg-menge,

End of it_tab. " This will give right results

Begin of it_tab occurs 0,

field1(4) type c,

field2(2) type n,

field3(6) type c,

menge like mseg-menge,

field4(4) type n,

End of it_tab. " The result won't be appropriate

0 Kudos

thanks for your reply

now wat to do in my code?