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

Doubt in abap report program.

0 Likes
944

Please can i get the logic without "GroupBY" or "Control break Statement" for the below picture.

2 REPLIES 2
Read only

Sourav1
Participant
0 Likes
743
1st Method:
LOOP AT lt_input INTO ls_input. AT NEW FIELD1. if ls_output-field2 IS NOT INITIAL. APPEND ls_output to lt_output. endif. LS_OUTPUT-FIELD1 = LS_INPUT-FIELD1. CLEAR: LS_OUTPUT-FIELD2. ENDAT. IF LS_OUTPUT-FIELD2 IS NOT INITIAL. LV_TEMP = LS_OUTPUT-FIELD2. CLEAR: LS_OUTPUT-FIELD2. CONCATENATE LV_TEMP ',' LS_INPUT-FIELD2 INTO LS_OUTPUT-FIELD2. ELSE. LS_OUTPUT-FIELD2 = LS_INPUT-FIELD2. ENDIF. ENDLOOP. 2nd Method
LOOP AT lt_input INTO ls_input. lv_Current_key = ls_input-field1. READ TABLE LT_OUTPUT ASSIGNING <FS_OUTPUT> WITH KEY field1 = lv_current_key. IF sy-subrc = 0. "Already exists CONCATENATE <FS_OUTPUT>-field2 ',' ls_input-field2 INTO lv_temp. <FS_OUTPUT>-field2 = lv_temp. ELSE. ls_output-field1 = lv_current_key. ls_output-field2 = ls_input-field2. APPEND ls_output to lt_output. ENDIF. ENDLOOP.
Read only

DoanManhQuynh
Active Contributor
743

Why would you want it hard way? GROUP BY or control break statement is the first thing should used in this case. anw, without it you can do as follow:

TYPES: BEGIN OF ty,
         f1 TYPE c,
         f2 TYPE c LENGTH 10,
       END OF ty.
DATA: tab1 TYPE TABLE OF ty,
      tab2 TYPE TABLE OF ty.
tab1 = VALUE #( ( f1 = 'a' f2 = 'x'  )
                ( f1 = 'b' f2 = 'y'  )
                ( f1 = 'b' f2 = 'z'  )
                ( f1 = 'b' f2 = 'w'  )
                ( f1 = 'c' f2 = 'v'  )
                ( f1 = 'c' f2 = 'u'  )
                ( f1 = 'd' f2 = 't'  )
                ( f1 = 'd' f2 = 's'  )
                ( f1 = 'e' f2 = 'q'  )
                ).
"Without group by
LOOP AT tab1 INTO DATA(wa1).
  READ TABLE tab2 ASSIGNING FIELD-SYMBOL(<wa2>) WITH KEY f1 = wa1-f1.
  "New
  IF sy-subrc <> 0.
    APPEND wa1 TO tab2.
    CONTINUE.
  ENDIF.
  "Update
  <wa2>-f2 = |{ <wa2>-f2 },{ wa1-f2 }|.
ENDLOOP.
"Write
LOOP AT tab1 INTO DATA(wa2) GROUP BY wa2-f1.
  DATA(str) = REDUCE string( INIT txt TYPE string
                             FOR grp IN GROUP wa2 NEXT txt = COND #( WHEN txt = space THEN |{ wa2-f1 }: { grp-f2 }|
                                                                     ELSE |{ txt },{ grp-f2 }| ) ).
  WRITE / str.
ENDLOOP.