0

I need help on achieving GROUP BY functionality for a dynamic table. Here I have just created a sample piece of code that is similar to my production code.

I have a database table that has more than 166 fields. Based on Factor_Type, the internal table will be created with selective fields according to the config table. Now I need to upload excel data into this dynamic internal table.

Here for example, below is the sample structure with few fields which looks similar to my DB table,

 TYPES: BEGIN OF ty_table,
             factor_type      TYPE char30,
             field1           TYPE char30,
             field2           TYPE char30,
             field3           TYPE char30,
             field4           TYPE char30,
             percentage_type1 TYPE i,
             percentage_type2 TYPE i,
           END OF ty_table.

Now when I upload the data from excel, based on FACTOR TYPE, I will be creating a dynamic internal table <FS_TABLE>.

My LOOP AT GROUP logic,

" get the key field type to local variable
DATA(lv_factor_type) = <fs_factor_type>.

LOOP AT <fs_table> ASSIGNING FIELD-SYMBOL(<fs_line>)
  GROUP BY SWITCH string( lv_factor_type WHEN 'TYPE1'
                                         THEN | FIELD1 = <FS_LINE>-FIELD1 |
                                         WHEN 'TYPE2' THEN | FIELD2 = <FS_LINE>-FIELD2 FIELD3 = <FS_LINE>-FIELD3 |   )
                         ASCENDING ASSIGNING FIELD-SYMBOL(<fs_group>).

  LOOP AT GROUP <fs_group> ASSIGNING FIELD-SYMBOL(<fs_mem>).
    <fs_table_temp> = VALUE #( BASE <fs_table_temp> ( <fs_mem> ) ).
  ENDLOOP.

  DATA(lv_total) = REDUCE i(  INIT value TYPE i
                                FOR <fs_val> IN  <fs_table_temp>
                                NEXT value = value + SWITCH #( lv_factor_type
                                                          WHEN 'TYPE1' THEN (`<fs_val>-percentage_type1`)
                                                          WHEN 'TYPE2' THEN (`<fs_val>-percentage_type2`) ) ) .


  IF lv_total > 100.
    " Append that to the export table with the FACTOR TYPE AND TOTAL
  ENDIF.

ENDLOOP.

Here,

  1. The group by clause should differ based on the FACTOR_TYPE. I have to make it dynamic since I have more than 50+ combinations of grouping based on Factor_Type.
  2. In the Reduce statement, I can't mention the fields PERCENTAGE_TYPE1 and PERCENTAGE_TYPE2 since <FS_VAL> is a dynamic structure.

Is there any way to achieve these?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    Please focus on one question only, for instance the first one. For this first question, it seems that you want to use something like [`ASSIGN COMPONENT (-factor_type) OF STRUCTURE `](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapassign_mem_area_dynamic_dobj.htm), there's no alternative to use it as an expression (i.e. you can't have it inside `GROUP BY`). – Sandra Rossi Aug 03 '21 at 09:33
  • Does this answer your question? [LOOP AT... GROUP BY with dynamic group key](https://stackoverflow.com/questions/62956466/loop-at-group-by-with-dynamic-group-key) – Suncatcher Aug 03 '21 at 13:39
  • @Suncatcher Tried that already. But it didnt worked. :( – HatriGt Ace Aug 03 '21 at 17:49
  • 1
    in the above question they had suggested solution via method and you used another approach in your question, if you tried the Philipp's approach and it "didn't work" you should post the question showing what exact error you are getting via method and what've you tried, now I don't see you tried anything – Suncatcher Aug 04 '21 at 07:18

0 Answers0