FUNCTION {{p}}bot_plan IMPORTING iv_min_hl TYPE decfloat34 EXPORTING ev_total_hl TYPE decfloat34 ev_plan_count TYPE int4. TYPES: BEGIN OF ty_group, beer_type TYPE {{p}}botord-beer_type, total_hl TYPE {{p}}botpln-total_hl, order_count TYPE {{p}}botpln-order_count, END OF ty_group. DATA lt_groups TYPE HASHED TABLE OF ty_group WITH UNIQUE KEY beer_type. DATA ls_group TYPE ty_group. DATA ls_row TYPE {{p}}botpln. DATA lt_plan TYPE STANDARD TABLE OF {{p}}botpln WITH EMPTY KEY. CLEAR: ev_total_hl, ev_plan_count. SELECT beer_type, volume_hl FROM {{p}}botord INTO TABLE @DATA(lt_orders). LOOP AT lt_orders INTO DATA(ls_order). IF ls_order-volume_hl <= 0. CONTINUE. ENDIF. READ TABLE lt_groups ASSIGNING FIELD-SYMBOL() WITH TABLE KEY beer_type = ls_order-beer_type. IF sy-subrc = 0. -total_hl = -total_hl + ls_order-volume_hl. -order_count = -order_count + 1. ELSE. ls_group = VALUE #( beer_type = ls_order-beer_type total_hl = ls_order-volume_hl order_count = 1 ). INSERT ls_group INTO TABLE lt_groups. ENDIF. ENDLOOP. LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(). IF -total_hl < iv_min_hl. CONTINUE. ENDIF. ls_row = VALUE #( client = sy-mandt beer_type = -beer_type plan_pos = 0 total_hl = -total_hl order_count = -order_count ). APPEND ls_row TO lt_plan. ev_total_hl = ev_total_hl + -total_hl. ENDLOOP. SORT lt_plan BY total_hl DESCENDING beer_type ASCENDING. LOOP AT lt_plan ASSIGNING FIELD-SYMBOL(). -plan_pos = sy-tabix. ENDLOOP. ev_plan_count = lines( lt_plan ). DELETE FROM {{p}}botpln. IF lt_plan IS NOT INITIAL. INSERT {{p}}botpln FROM TABLE @lt_plan. ENDIF. ENDFUNCTION.