Skip to Content
author's profile photo Former Member
Former Member

A sample code to get record count from BSEG based on the BKPF values?

hi ABAP4 experts,

We are trying to know how many records in BSEG(Accounting Document Segment) based on the parameter values of BKPF(Accounting Document Header). The parameter values of BKPF we know are:

1. Company Code (BUKRS) range from Z100 to Z500

2. Document type (BLART) need to input so many ranges:

ZA

ZE

ZT

ZG

Y2

Y4

U4

U8

3. Doc.status (BSTAT) is not equal to 'S'

Based on the three conditions in BKPF, we would like to know record count in BSEG. We will be very appreciated if some experts here give us the sample code and we sure will give you reward points.

Thanks in advance!

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    Posted on Mar 25, 2006 at 05:24 PM

    Hi Kevin,

    You code like this.

    DATA: BEGIN OF ITAB2 OCCURS 0,
            BELNR LIKE BSEG-BELNR,
            BURKS LIKE BSEG-BURKS, 
            GJAHR LIKE BSEG-GJAHR,
            BUZEI LIKE BSEG-BUZEI,
            WRBTR LIKE BSEG-WRBTR,
            <b>SHKZG like bseg-SHKZG.</b>
    DATA: END OF ITAB2.
     
     
    DATA: WA_WRBTR TYPE BSEG-WRBTR,
          WA_COUNT TYPE I.
     
     
    SELECT BELNR BURKS GJAHR BUZEI WRBTR SHKZG
    INTO TABLE ITAB2 
    FROM BSEG
    FOR ALL ENTRIES IN ITAB1
    WHERE BELNR = ITAB1-BELNR
      AND BURKS = ITAB1-BURKS
      AND GJAHR = ITAB1-GJAHR.
     
    LOOP AT ITAB2.
        WA_COUNT = WA_COUNT + 1.
            
        <b>IF ITAB2-SHKZG = 'S'. 
          WA_WRBTR = WA_WRBTR + ITAB2-WRBTR * -1.
        ELSE.
         WA_WRBTR = WA_WRBTR + ITAB2-WRBTR.
        ENDIF.</b>
     ENDLOOP. 
     
    WRITE: / 'TOTAL BSEG-WRBTR:', WA_WRBTR,
             'TOTAL COUNT:', WA_COUNT.

    Hope this will help.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 25, 2006 at 03:18 AM

    hi

    first use the select on the BKPF table based on your condition and populate an internal table ..

    populate the ranges..

    RANGES R_BUKRS FOR BKPF-BUKRS.
    RANGES R_BLART  FOR BKPF_BLART.
    
    
    R_bukrs-SIGN = 'I'.
    R_bukrs-OPTION = 'BT'.
    R_bukrs-LOW = 'Z100'.
    R_bukrs-HIGH = 'Z500'.
    APPEND R_bukrs.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'ZA'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'ZE'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'ZT'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'ZG'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'Y2'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'Y4'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'U4'.
    APPEND R_blart.
    
    R_blart-SIGN = 'I'.
    R_blart-OPTION = 'EQ'.
    R_blart-LOW = 'U8'.
    APPEND R_blart.
    
     
    select bukrs blart bstat 
      into table itab1 
      from BKPF 
      where BUKRS in R_bukrs
      and BLART in R_blart
      and BSTAT NE 's'.
    
    SELECT count(*) 
      into count 
     from BSEG
     FOR ALL ENTRIES in itab1.

    regards

    satesh

    Message was edited by: Satesh R

    Message was edited by: Satesh R

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      hi Kevin,

      for that you can do like this..

      data : itab2 like table of BSEG with header line.
      SELECT *
      into TABLE itab
      from BSEG
      FOR ALL ENTRIES in itab1
      where belnr = itab1-belnr
      and bukrs = itab1-bukrs
      and gjahr = itab1-gjahr.
      
      loop at itab2.
       sum.
       GL_count = GL_count + 1.
      endloop.
      total = itab2-WRBTR.

      but this wud have a poor performance..

      regards

      satesh

  • Posted on Mar 25, 2006 at 02:51 PM

    Hi Kevin,

    You can code like this.

    data: WRBTR type BSEG-WRBTR.
    
    
    SELECT count(*) sum ( WRBTR )
    into (count, WRBTR) 
    from BSEG
    FOR ALL ENTRIES in itab1
    where belnr = itab1-belnr
    and bukrs = itab1-bukrs
    and gjahr = itab1-gjahr.

    Hope this will help.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi again,

      1. using SUM directly in sql,

      won't work bcos

      BSEG is a pooled table

      and abap will give error as :

      <b>Aggregate functions and the addition DISTINCT are not supported in field lists for pooled and cluster tables. </b>

      regards,

      amit m.

  • Posted on Mar 25, 2006 at 03:44 PM

    Hi Kevin,

    Apologies ... I do not have SAP system now therefore I can not do syntac check.

    I believe you can code like this.

    DATA: BEGIN OF ITAB2 OCCURS 0,
            BELNR LIKE BSEG-BELNR,
            BURKS LIKE BSEG-BURKS, 
            GJAHR LIKE BSEG-GJAHR,
            WRBTR LIKE BSEG-WRBTR.
    DATA: END OF ITAB2.
    
    
    DATA: WA_WRBTR TYPE BSEG-WRBTR,
          WA_COUNT TYPE I.
    
    
    SELECT BELNR BURKS GJAHR WRBTR 
    INTO TABLE ITAB2 
    FROM BSEG
    FOR ALL ENTRIES IN ITAB1
    WHERE BELNR = ITAB1-BELNR
      AND BURKS = ITAB1-BURKS
      AND GJAHR = ITAB1-GJAHR.
    
    LOOP AT ITAB2.
      WA_WRBTR = WA_WRBTR + ITAB2-WRBTR.
      WA_COUNT = WA_COUNT + 1.
    ENDLOOP.
    
    
    WRITE: / 'TOTAL BSEG-WRBTR:', WA_WRBTR,
             'TOTAL COUNT:', WA_COUNT.
    

    Hope this will help.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      hi Ferry/Satesh,

      Actually your two codes give the same effect, but now the count is different and incorrect now.

      The correct count with the following code:

      SELECT count(*)

      into GL_count

      from BSEG

      FOR ALL ENTRIES in itab1

      where belnr = itab1-belnr

      and bukrs = itab1-bukrs

      and gjahr = itab1-gjahr.

      is correct and the count is 607, but now after using the following code:

      SELECT BELNR BURKS GJAHR WRBTR

      INTO TABLE ITAB2

      FROM BSEG

      FOR ALL ENTRIES IN ITAB1

      WHERE BELNR = ITAB1-BELNR

      AND BURKS = ITAB1-BURKS

      AND GJAHR = ITAB1-GJAHR.

      LOOP AT ITAB2.

      WA_WRBTR = WA_WRBTR + ITAB2-WRBTR.

      WA_COUNT = WA_COUNT + 1.

      ENDLOOP.

      WRITE: / 'TOTAL BSEG-WRBTR:', WA_WRBTR,

      'TOTAL COUNT:', WA_COUNT.

      The count is 397 which is incorrect.

      How to resolve the above problem?

      Looking forward to seeing your answers soon!

  • Posted on Mar 25, 2006 at 04:40 PM

    Hi Kevin,

    Do you know the key fields of table BSEG? I don't have SAP system now. I believe my code did not pass enough key field to count number of records. Therefore, system passed less records.

    Perhaps like this,

    DATA: BEGIN OF ITAB2 OCCURS 0,
            BELNR LIKE BSEG-BELNR,
            BURKS LIKE BSEG-BURKS, 
            GJAHR LIKE BSEG-GJAHR,
            <b>BUZEI LIKE BSEG-BUZEI,</b>
            WRBTR LIKE BSEG-WRBTR.
    DATA: END OF ITAB2.
     
     
    DATA: WA_WRBTR TYPE BSEG-WRBTR,
          WA_COUNT TYPE I.
     
     
    SELECT BELNR BURKS GJAHR <b>BUZEI</b> WRBTR 
    INTO TABLE ITAB2 
    FROM BSEG
    FOR ALL ENTRIES IN ITAB1
    WHERE BELNR = ITAB1-BELNR
      AND BURKS = ITAB1-BURKS
      AND GJAHR = ITAB1-GJAHR.
     
    LOOP AT ITAB2.
        WA_COUNT = WA_COUNT + 1.
        WA_WRBTR = WA_WRBTR + ITAB2-WRBTR.
    ENDLOOP.
     
     
    WRITE: / 'TOTAL BSEG-WRBTR:', WA_WRBTR,
             'TOTAL COUNT:', WA_COUNT

    Also when you sum WRBTR, please include field to determine debit/credit sign.

    Hope this will help.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 25, 2006 at 05:30 PM

    Glad to help you.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      hi Ferry,

      If we just want to check how many records and calculate a total amount for a specific field, e.g., DMBTR in a system table, e.g., BSEG. Note: there is no any selection for this table BSEG, we just want to get the total record count in this table and also the total amount for a specific field e.g. DMBTR in this table.

      Do we have to use an internal table to transfer all the records of BSEG into the internal table to get the result? Also would be appreciated if you can provide the code!

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.