我有以下内部表格:
身份证 | 文字 | 起源 |
---|---|---|
1 | 文字1 | 词 |
1 | 文字1 | 词 |
1 | 文字1 | Excel |
1 | 文字1 | 幻灯片 |
2 | 文字1 | Excel |
2 | 文字1 | 幻灯片 |
3 | 文字1 | 幻灯片 |
3 | 文字1 | Excel |
3 | 文字1 | Excel |
3 | 文字1 | Excel |
我正在努力得到结果:
身份证 | 起源 | 分类 |
---|---|---|
1 | 词 | 三重_Word_Excel_PowerPoint |
1 | 词 | 三重_Word_Excel_PowerPoint |
1 | Excel | 三重_Word_Excel_PowerPoint |
1 | 幻灯片 | 三重_Word_Excel_PowerPoint |
2 | Excel | Double_Excel_PowerPoint |
2 | 幻灯片 | Double_Excel_PowerPoint |
3 | 词 | 双Excel_Word |
3 | Excel | 双Excel_Word |
3 | Excel | 双Excel_Word |
3 | Excel | 双Excel_Word |
我尝试使用循环调用来过滤 ID,并检查 Origin 列中存在哪个值。但我就是无法理解。如果有更好的解决方案,在内表上使用 SELECT 调用,那么我也可以尝试一下。
我的代码示例:
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.
简单的选择是将 ID 和分类组合单独生成到另一个内部表中,并在循环初始表时使用它来构造最终表。 代码片段如下所示。
REPORT zabap_sql.
TYPES BEGIN OF ty1.
TYPES id TYPE i.
TYPES text TYPE char10.
TYPES origin TYPE char10.
TYPES END OF ty1.
TYPES BEGIN OF ty_cls.
TYPES id TYPE i.
TYPES classification TYPE string.
TYPES END OF ty_cls.
TYPES BEGIN OF tyresult.
TYPES id TYPE i.
TYPES origin TYPE char10.
TYPES classification TYPE char50.
TYPES END OF tyresult.
DATA lt_cls TYPE STANDARD TABLE OF ty_cls.
DATA ls_cls TYPE ty_cls.
DATA lv_id_string TYPE string.
DATA lv_id_count TYPE i.
DATA lt_internal_table TYPE STANDARD TABLE OF ty1.
DATA lt_result TYPE STANDARD TABLE OF tyresult.
START-OF-SELECTION.
lt_internal_table = VALUE #(
( id = 1 text = 'Text1' origin = 'Word' )
( id = 1 text = 'Text1' origin = 'Excel' )
( id = 1 text = 'Text1' origin = 'PowerPoint' )
( id = 1 text = 'Text1' origin = 'Word' )
( id = 2 text = 'Text1' origin = 'Excel' )
( id = 2 text = 'Text1' origin = 'PowerPoint' )
( id = 3 text = 'Text1' origin = 'Excel' )
( id = 3 text = 'Text1' origin = 'Word' )
( id = 3 text = 'Text1' origin = 'Excel' )
( id = 3 text = 'Text1' origin = 'Excel' )
).
SELECT FROM @lt_internal_table AS lt
FIELDS id, origin
GROUP BY id, origin
ORDER BY id,origin
INTO TABLE @DATA(lt_temp) .
LOOP AT lt_temp INTO DATA(ls_temp) GROUP BY ls_temp-id.
LOOP AT GROUP ls_temp INTO DATA(ls_temp_groupped).
lv_id_count = lv_id_count + 1.
IF lv_id_string IS INITIAL.
lv_id_string = ls_temp_groupped-origin.
ELSE.
CONCATENATE lv_id_string ls_temp_groupped-origin INTO lv_id_string SEPARATED BY '_'.
ENDIF.
ENDLOOP.
CASE lv_id_count.
WHEN 1. CONCATENATE 'Single' lv_id_string INTO lv_id_string SEPARATED BY '_'.
WHEN 2. CONCATENATE 'Double' lv_id_string INTO lv_id_string SEPARATED BY '_'.
WHEN 3. CONCATENATE 'Triple' lv_id_string INTO lv_id_string SEPARATED BY '_'.
ENDCASE.
ls_cls = VALUE #( id = ls_temp-id classification = lv_id_string ).
APPEND ls_cls TO lt_cls.
CLEAR: lv_id_count, lv_id_string.
ENDLOOP.
SORT lt_cls BY id.
LOOP AT lt_internal_table INTO DATA(ls_data).
READ TABLE lt_cls INTO DATA(ls_cls1) WITH KEY id = ls_data-id.
" Save result in internal result table
APPEND VALUE #( id = ls_data-id origin = ls_data-origin classification = ls_cls1-classification ) TO lt_result.
ENDLOOP.
cl_demo_output=>display_data( lt_result ).
输出如下。
ID ORIGIN CLASSIFICATION
1 Word Triple_Excel_PowerPoint_Word
1 Excel Triple_Excel_PowerPoint_Word
1 PowerPoint Triple_Excel_PowerPoint_Word
1 Word Triple_Excel_PowerPoint_Word
2 Excel Double_Excel_PowerPoint
2 PowerPoint Double_Excel_PowerPoint
3 Excel Double_Excel_Word
3 Word Double_Excel_Word
3 Excel Double_Excel_Word
3 Excel Double_Excel_Word
您的代码无法编译,所以我猜您已经编写了代码而没有验证它。请修复它。
它也远非最小,例如根据您提供的信息,不需要输入文本。
你的问题并不清楚你目前获得了什么结果。
请注意,我建议您首先专注于生成中间结果(最终结果是显而易见的),这也会简化您的问题:
1 Triple_Word_Excel_PowerPoint
2 Double_Excel_PowerPoint
3 Double_Excel_Word
如果您错过了几行明显的代码,则此代码将是一个有效的最小可重现示例。正如您所看到的,它只为 ID 1 生成数据,因此快速基本调试会告诉您问题出在阻止处理多个 ID 的条件上:
REPORT.
TYPES: BEGIN OF ty_input,
id TYPE i,
origin TYPE string,
END OF ty_input.
TYPES: BEGIN OF ty_result,
id TYPE i,
origin TYPE string,
classification TYPE string,
END OF ty_result.
TYPES ty_input_table TYPE STANDARD TABLE OF ty_input WITH EMPTY KEY.
TYPES ty_result_table TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY.
DATA lv_current_id TYPE string VALUE ' '.
DATA lv_origin_count TYPE i VALUE 0.
DATA lv_origin_text TYPE string VALUE ' '.
DATA lt_origin TYPE RANGE OF string.
DATA(lt_internal_table) = VALUE ty_input_table( ( id = 1 origin = 'Word ' )
( id = 1 origin = 'Word ' )
( id = 1 origin = 'Excel ' )
( id = 1 origin = 'PowerPoint' )
( id = 2 origin = 'Excel ' )
( id = 2 origin = 'PowerPoint' )
( id = 3 origin = 'PowerPoint' )
( id = 3 origin = 'Excel ' )
( id = 3 origin = 'Excel ' )
( id = 3 origin = 'Excel ' ) ).
DATA(lt_result) = VALUE ty_result_table( ).
APPEND VALUE #( sign = 'I'
option = 'EQ'
low = space ) TO lt_origin.
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 VALUE #( sign = 'I'
option = 'EQ'
low = ls_data-origin ) TO lt_origin.
" Count occurrence of origin per id
LOOP AT lt_internal_table INTO DATA(ls_data2) WHERE id = ls_data-id AND origin = ls_data-origin.
lv_origin_count = lv_origin_count + 1.
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
origin = ls_data-origin
classification = lv_origin_text ) TO lt_result.
ENDIF.
ENDLOOP.
当前生成此结果表:
Row ID ORIGIN CLASSIFICATION
=====================================
1 1 Word DoubleWordWord
2 1 Excel SingleExcel
3 1 PowerPoint SinglePowerPoint