on 02-19-2018 5:41 PM
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.
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( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Use HASHED table and keep description as key. when you are appending records to internal table use COLLECT.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Read [again?] documentation on LOOP and AT (group) statements, then can you guess what should be done
If and only if mastered (and if recent version of Abap) read the LOOP AT itab - GROUP BY
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't dare to recommend LOOP AT GROUP BY to you. Use IF?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Hello Brian,
it not BAD, just.. more powerful. You might solve your task with
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
User | Count |
---|---|
91 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.