TYPES: BEGIN OF ty_itab, begda TYPE d, endda TYPE d, orgeh TYPE char20, END OF ty_itab. TYPES: BEGIN OF ty_result, begda TYPE d, endda TYPE d, orgeh TYPE char100, END OF ty_result. DATA it_itab TYPE TABLE OF ty_itab. DATA wa_itab TYPE ty_itab. DATA it_itab1 TYPE TABLE OF ty_itab. "无重叠时间段 DATA it_itab2 TYPE TABLE OF ty_itab. "重叠时间段 DATA it_result TYPE TABLE OF ty_result. "最终结果表 DATA wa_result TYPE ty_result. DATA it_result1 TYPE TABLE OF ty_result. "临时结果表 DATA wa_result1 TYPE ty_result. DATA it_itab3 TYPE TABLE OF ty_itab. DATA begda TYPE d. DATA endda TYPE d.
DATA: flag1(1) TYPE c, flag2(1) TYPE c.
SELECTION-SCREEN PUSHBUTTON 2(10) but1 USER-COMMAND cli1. PARAMETERS: p_begda TYPE sy-datum. PARAMETERS: p_endda TYPE sy-datum. PARAMETERS: p_orgeh TYPE char20.
IF it_itab IS INITIAL. it_itab[] = it_itab2[]. ENDIF.
CLEAR it_itab2.
WRITE:/ '输入的时间段:'. LOOP AT it_itab INTO wa_itab. WRITE:/ wa_itab-begda,wa_itab-endda,wa_itab-orgeh. ENDLOOP.
WRITE: / '区分重叠与不重叠部分'.
SORT it_itab BY begda. LOOP AT it_itab INTO wa_itab. IF sy-tabix = 1. begda = wa_itab-begda. endda = wa_itab-endda. APPEND wa_itab TO it_itab1. ELSE. IF wa_itab-begda >= begda AND wa_itab-begda <= endda. * 重叠 APPEND wa_itab TO it_itab2. ELSE. begda = wa_itab-begda. endda = wa_itab-endda. APPEND wa_itab TO it_itab1. ENDIF. ENDIF. ENDLOOP.
WRITE : / '不重叠部分:'. LOOP AT it_itab1 INTO wa_itab. WRITE:/ wa_itab-begda,wa_itab-endda,wa_itab-orgeh. ENDLOOP. WRITE : / '重叠部分:'. LOOP AT it_itab2 INTO wa_itab. WRITE:/ wa_itab-begda,wa_itab-endda,wa_itab-orgeh. ENDLOOP.
* 将重叠部分逐一加入不重叠部分以拆分数据 * 不重叠部分先放入结果 LOOP AT it_itab1 INTO wa_itab. MOVE-CORRESPONDING wa_itab TO wa_result. APPEND wa_result TO it_result. ENDLOOP. WRITE:/ '逐一处理重叠部分'. LOOP AT it_itab2 INTO wa_itab. WRITE:/ '第' , sy-tabix, '条重叠数据'. CLEAR it_itab3. APPEND wa_itab TO it_itab3. PROVIDE FIELDS orgeh FROM it_result INTO wa_result VALID flag1 BOUNDS begda AND endda FIELDS orgeh FROM it_itab3 INTO wa_itab VALID flag2 BOUNDS begda AND endda BETWEEN '18000101' AND '99991231'.
wa_result1-begda = wa_result-begda. wa_result1-endda = wa_result-endda. IF flag2 = 'X' AND flag1 = 'X'. wa_result1-orgeh = wa_result-orgeh && '/' && wa_itab-orgeh. ELSEIF flag2 = 'X' AND flag1 NE 'X'. wa_result1-orgeh = wa_itab-orgeh. ELSE. wa_result1-orgeh = wa_result-orgeh. ENDIF. APPEND wa_result1 TO it_result1. * SKIP. ENDPROVIDE.
LOOP AT it_result1 INTO wa_result1. WRITE:/ wa_result1-begda,wa_result1-endda,wa_result1-orgeh. ENDLOOP. it_result[] = it_result1[]. CLEAR it_result1[]. ENDLOOP.
WRITE:/ '最终结果'. LOOP AT it_result INTO wa_result. WRITE:/ wa_result-begda,wa_result-endda,wa_result-orgeh. ENDLOOP.