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,
- 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 onFactor_Type
. - In the Reduce statement, I can't mention the fields
PERCENTAGE_TYPE1
andPERCENTAGE_TYPE2
since <FS_VAL> is a dynamic structure.
Is there any way to achieve these?