‎2016 Jan 08 1:45 PM
Hello gurus,
this is the situation. I have following table (tab_1)
| matnr | month | value_day1 | value_day2 | value_day3 | value_day4 |
|---|---|---|---|---|---|
| 0001 | 01 | 3 | 5 | 7 | 9 |
| 0001 | 02 | 6 | 7 | 5 | 0 |
| 0001 | 03 | 5 | 2 | 5 | 0 |
| 0002 | 01 | 1 | 2 | 4 | 10 |
Well, I want to create a new table with (tab_2)
| matnr | month | sum |
|---|---|---|
| 0001 | 01 | 24 |
| 0001 | 02 | 18 |
| 0001 | 03 | 12 |
| 0002 | 01 | 17 |
After the select to extract tab_1, the code for tab_2 is
LOOP AT gt_tab_1 INTO gs_tab_1.
gs_tab_2-matnr = gs_tab_1-matnr.
gs_tab_2-month = gs_tab_1-month.
gs_tab_2-sum = gs_tab_1-value_day1 + gs_tab_1-value_day2 + gs_tab_1-value_day3 + gs_tab_1-value_day4.
APPEND gs_tab_2 TO gt_tab_2.
ENDLOOP.
What do you think? Is the code correct?
thank you guys
‎2016 Jan 08 2:04 PM
Hi Giuliano,
This is one way todo it and i think would work just fine if you have a static number of day columns that you will be working with. If you have a larger list of columns or if this column list can grow or shrink then this would not be the best way to go and would want to do a more dynamic approach to summing up the necessary fields.
I've also read that it's possible to do this in sql itself using oracle commands like concat and group_concat but i haven't been able to do a POC in sap yet.
Geoffery
‎2016 Jan 08 2:04 PM
Hi Giuliano,
This is one way todo it and i think would work just fine if you have a static number of day columns that you will be working with. If you have a larger list of columns or if this column list can grow or shrink then this would not be the best way to go and would want to do a more dynamic approach to summing up the necessary fields.
I've also read that it's possible to do this in sql itself using oracle commands like concat and group_concat but i haven't been able to do a POC in sap yet.
Geoffery
‎2016 Jan 08 3:28 PM
Hello Geoffery,
and thanks for the answer. I'd like to modify the code using the fileds symbols but i'm not very good at with them. Do you have some code-example or other topics to show me?
Have a good day.
‎2016 Jan 08 3:41 PM
‎2016 Jan 08 2:44 PM
‎2016 Jan 08 2:50 PM
Hello Rob,
I'd just like to know if the code would be ok or there are some tips to improve performace and staff like this. I tried this code and I have the correct result...of course I hav ea very few examples.
thank you
‎2016 Jan 08 2:51 PM
I won't answer to the question too.
Just one suggestion, as the DO VARYING syntax is now obsolete, you could "play" with some ASSIGN COMPONENT OF STRUCTURE to loop between the day fields and calculate the sum in a dynamic mode. (Of course you have the right to prefer field names as gs_tab_1-value_day31)
Regards,
Raymond
‎2016 Jan 08 2:55 PM
Sorry Raymond if my question is a bit rude, it's my first post. Thank you for your answer and I'd like to ask you if you have some example about your suggestion.
thank you
‎2016 Jan 08 3:28 PM
Your presented code is fine (except your are using prefixes which are now officially (i.e. by Horst Kellar) not recommended!
What Raymond suggests is if you want to work with tables generically, when you don't know how many value fields there are - or you want to not have to change the code, if the table structure gains extra value fields.
As for the code to do it - please try it yourself. You've been given the abap keyword that will do it - now do some reading! It's the best way to learn.
‎2016 Jan 08 3:31 PM
‎2016 Jan 08 3:47 PM
‎2016 Jan 08 3:53 PM
‎2016 Jan 08 3:56 PM
‎2016 Jan 08 4:58 PM
How about creating an extra column in tab_1 and populating the column when you append the row?