Skip to Content
-2

Sum a Group of Records in an Internal Table into a Single Record

Hi ABAPers,

I have an internal table with records as seen below:

Description----------Quantity----------Price

1. Daily-------------------1000--------------52.75

2. Daily-------------------1500-------------52.75

3. Daily-------------------1700--------------52.75

4. Saturday--------------3000-------------55.00

5. Sunday----------------4000-------------55.00

Now I need to combine the records with description 'Daily' into a single record in the internal table, with the quantity field being a sum of the 3 records.

I've tried using this:

LOOP AT lt_tab into ls_tab.

AT NEW description.
daily_total = daily_total + ls_tab-menge
ENDAT.

But it's not giving the desired results.

Any idea on how to go about this?

Thanks.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Feb 19 at 09:09 PM
    -1

    define LT_SUM the same as LT_TAB.

    LOOP AT LT_TAB INTO LS_TAB.

    COLLECT LS_TAB INTO LT_SUM.

    ENDLOOP.

    This will result in a table that has one entry per description with the number fields summed. Keep in mind that the price will also be added in this way so you will have price with 52.75 X 3 in the instance you outlined above.

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 20 at 07:37 PM

    Use HASHED table and keep description as key. when you are appending records to internal table use COLLECT.

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 20 at 11:16 AM

    you can try below code. Hopefully it will assist.

    CLASS demo DEFINITION.
    PUBLIC SECTION.
    CLASS-METHODS:
    main,
    initialize.
    PRIVATE SECTION.
    TYPES: BEGIN OF ty_data,
    desc TYPE c LENGTH 30,
    qty TYPE i,
    price TYPE p LENGTH 10 DECIMALS 2,
    END OF ty_data.

    CLASS-DATA
    t_data TYPE STANDARD TABLE OF ty_data.
    ENDCLASS.

    CLASS demo IMPLEMENTATION.
    METHOD main.
    initialize( ).

    DATA(out) = cl_demo_output=>new( ).

    TYPES:
    BEGIN OF ty_sum,
    desc TYPE c LENGTH 30,
    qty TYPE i,
    END OF ty_sum.
    DATA t_sum TYPE STANDARD TABLE OF ty_sum.
    DATA s_sum LIKE LINE OF t_sum.

    LOOP AT t_data ASSIGNING FIELD-SYMBOL(<wa>)
    GROUP BY ( key = <wa>-desc )
    ASCENDING
    ASSIGNING FIELD-SYMBOL(<group_key>).
    CLEAR s_sum.
    LOOP AT GROUP <group_key> ASSIGNING FIELD-SYMBOL(<line>).
    s_sum-desc = <line>-desc .
    s_sum-qty = s_sum-qty + <line>-qty.
    ENDLOOP.
    APPEND s_sum TO t_sum.
    ENDLOOP.
    out->write( t_sum ).
    out->display( ).
    ENDMETHOD.
    METHOD initialize.
    t_data = VALUE #( ( desc = 'Daily' qty = 1000 price = '52.75' )
    ( desc = 'Daily' qty = 1500 price = '52.75' )
    ( desc = 'Daily' qty = 1700 price = '52.75' )
    ( desc = 'Saturday' qty = 3000 price = '55.00' )
    ( desc = 'Sunday' qty = 4000 price = '55.00' ) ).

    ENDMETHOD.
    ENDCLASS.

    START-OF-SELECTION.

    demo=>main( ).

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 19 at 06:01 PM

    I don't dare to recommend LOOP AT GROUP BY to you. Use IF?

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 20 at 07:44 AM

    Read [again?] documentation on LOOP and AT (group) statements, then can you guess what should be done

    • in a AT NEW/ENDAT block
    • out of a AT block
    • in a AT END OF/ENDAT block

    If and only if mastered (and if recent version of Abap) read the LOOP AT itab - GROUP BY

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 20 at 06:31 PM

    Sort itab by description.

    Clear daily_total.

    Read itab transporting no field with key description = 'Daily'.

    If sy-subrc is initial.

    Loop at itab into wa from sy-tabix.

    If wa-description NE 'Daily'.

    Exit.

    Endif.

    daily_total = daily_total + wa-menge.

    Endloop.

    Endif.

    Add comment
    10|10000 characters needed characters exceeded