I have the following internal table:
| ID | Text | Origin |
|---|---|---|
| 1 | Text1 | Word |
| 1 | Text1 | Word |
| 1 | Text1 | Excel |
| 1 | Text1 | PowerPoint |
| 2 | Text1 | Excel |
| 2 | Text1 | PowerPoint |
| 3 | Text1 | PowerPoint |
| 3 | Text1 | Excel |
| 3 | Text1 | Excel |
| 3 | Text1 | Excel |
and Im trying to get result:
| ID | Origin | Classification |
|---|---|---|
| 1 | Word | Triple_Word_Excel_PowerPoint |
| 1 | Word | Triple_Word_Excel_PowerPoint |
| 1 | Excel | Triple_Word_Excel_PowerPoint |
| 1 | PowerPoint | Triple_Word_Excel_PowerPoint |
| 2 | Excel | Double_Excel_PowerPoint |
| 2 | PowerPoint | Double_Excel_PowerPoint |
| 3 | Word | Double_Excel_Word |
| 3 | Excel | Double_Excel_Word |
| 3 | Excel | Double_Excel_Word |
| 3 | Excel | Double_Excel_Word |
I tried to filter the ID using a loop call and check which value is present in the Origin column. But I just can't get it. If there is a better solution using a SELECT call on the internal table, then i could try that as well.
My Code example:
DATA: lv_current_id TYPE string VALUE ' ',
lv_origin_count TYPE i VALUE 0,
lv_origin_text TYPE string VALUE ' '.
LOOP AT lt_internal_table INTO DATA(ls_data).
CLEAR: lv_origin_count, lv_origin_text.
" Check, if origin is processed
IF NOT ( ls_data-origin IN lt_origin ).
" Save origin in temporary list
APPEND ls_data-origin TO lt_origin.
" Count occurrence of origin per id
LOOP AT lt_data INTO DATA(ls_data2) WHERE id = ls_data-id AND origin = ls_data-origin.
ADD 1 TO lv_origin_count.
ENDLOOP.
" Create cext for classification columnText für die Anzahl_Herkunft erstellen
CASE lv_origin_count.
WHEN 1.
lv_origin_text = 'Single' && ls_data-origin.
WHEN 2.
lv_origin_text = 'Double' && ls_data-origin && ls_data-origin.
WHEN 3.
lv_origin_text = 'Triple' && ls_data-origin && ls_data-origin && ls_data-origin.
ENDCASE.
" Save result in internal result table
APPEND VALUE #( id = ls_data-id text = ls_data-text origin = ls_data-origin classification = lv_origin_text ) TO lt_result.
ENDIF.
ENDLOOP.
Simple option would be to generate ID & Classification combination separately into another internal table and use it while looping over the initial table to construct final table. Code snippet is as shown below.
Output is as below.