Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Sum several column into a single variable

Former Member
0 Likes
2,288

Hello gurus,

this is the situation. I have following table (tab_1)

matnrmonthvalue_day1value_day2value_day3value_day4
0001013579
0001026750
0001035250
00020112410

Well, I want to create a new table with (tab_2)

matnrmonthsum
00010124
00010218
00010312
00020117

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,243

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

13 REPLIES 13
Read only

Former Member
0 Likes
2,244

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

Read only

0 Likes
2,243

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.

Read only

0 Likes
2,243

Hi Giuliano,

Here is a good SCN thread on how to use field symbols with some very good feedback comments to provide further insight.

Using field symbols are not to difficult just takes a little getting used to.

Geoffery

Read only

Former Member
0 Likes
2,243

What do you think? Do you get the correct results?

Rob

Read only

0 Likes
2,243

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

Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,243

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

Read only

0 Likes
2,243

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

Read only

0 Likes
2,243

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.

Read only

0 Likes
2,243

Thank you Mattew I'm looking for some example

Read only

0 Likes
2,243

Hi there.


Matthew Billingham wrote:

Your presented code is fine (except your are using prefixes which are now officially (i.e. by Horst Kellar) not recommended!

Can please point me to to the post by

Regards.

Read only

0 Likes
2,243

It's a book. Detail here:

It's not that all prefixes are non-de-rigeur, just the ones that say that it's a structure/object reference/table...

Read only

0 Likes
2,243

Hi,

That was fast

Thanks.

Read only

Former Member
0 Likes
2,243

How about creating an extra column in tab_1 and populating the column when you append the row?