Skip to Content
-2

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

Feb 19 at 05:41 PM

131

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Only if the prerequisites are fulfilled.

0
Pratik Kasralikar Feb 20 at 07:37 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Umar Abdullah Feb 20 at 11:16 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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.

2
Jelena Perfiljeva

Couldn't agree more!

0
Horst Keller
Feb 19 at 06:01 PM
0

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

Show 6 Share
10 |10000 characters needed characters left characters exceeded

You mean

IF description = 'Daily'.
  total = total + ls_tab-quantity
ENDIF.
MODIFY lt_tab FROM ls_tab.
0

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

0

Why is it BAD to use LOOP AT GROUP BY?

0

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.

2

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

0

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.

0
Raymond Giuseppi
Feb 20 at 07:44 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Feb 20 at 06:31 PM
0

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.

Share
10 |10000 characters needed characters left characters exceeded