cancel
Showing results for 
Search instead for 
Did you mean: 

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

brian_mogambi
Explorer

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.

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member

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( ).

Jelena
Active Contributor

Please use CODE button to format the code. Long pieces of code posted as plain text are unreadable.

As a side note though, it's nice of you to write such a long piece of code but I feel it's more beneficial to guide OP so that they could write the code themselves. "Teach a man to fish", so to say. If we just copy-paste the code then we won't learn much, I'm afraid.

Thank you.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Couldn't agree more!

roheel79
Discoverer
0 Kudos

excellent job.

kasralikarpratik
Explorer
0 Kudos

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

jyoti_mandal
Explorer
0 Kudos

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.

raymond_giuseppi
Active Contributor
0 Kudos

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

thomas_lambert
Explorer
0 Kudos

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.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Only if the prerequisites are fulfilled.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

brian_mogambi
Explorer
0 Kudos

You mean

IF description = 'Daily'.
  total = total + ls_tab-quantity
ENDIF.
MODIFY lt_tab FROM ls_tab.
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Yes, at least partly. But the position of MODIFY?

brian_mogambi
Explorer
0 Kudos

Why is it BAD to use LOOP AT GROUP BY?

horst_keller
Product and Topic Expert
Product and Topic Expert

It is not bad at all, but since you don't understand how AT NEW works, I don't want to recomment an even more advanced concept to you.

nomssi
Active Contributor
0 Kudos

Hello Brian,

it not BAD, just.. more powerful. You might solve your task with

  • LOOP with IF
  • LOOP with AT NEW / AT END
  • LOOP with COLLECT
  • LOOP with GROUP BY
  • Expressions (FOR / GROUP BY / REDUCE)

Since you did not present working code, it is difficult to decide which approach to propose. LOOP with IF is the simplest thing that could possibly work. When you get this working, feel free to ask for alternatives.

JNN

BaerbelWinkler
Active Contributor
0 Kudos

LOOP with AT NEW / AT END OF could also easily be combined with SUM. Which will give the accumulated values for all applicable fields in the itab's structure.