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

to delete records whose dates are not overlapping

Former Member
0 Likes
1,594

Hi All,

I have an internal table.

two of the fields are

FROM TO

7.7.2008 8.7.2008

7.7.2008 7.7.2008

9.7.2008 10.7.2008 is to be deleted(not overlapping with other dates)

8.7.2008 8.7.2008

11.7.2008 13.7.2008

12.7.2008 14.7.2008

Now I have to delete the records whose dates do not overlap.

i.e each and every record has to be compared with each each and every other record.

As per the above data, my final internal table should have

FROM TO

7.7.2008 8.7.2008

7.7.2008 7.7.2008

8.7.2008 8.7.2008

11.7.2008 13.7.2008

12.7.2008 14.7.2008

How do you achieve it?

Thanks,

Sandeep.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,300

Hello Sandeep

Assuming that there are no duplicated records in your dates itab then sample report ZUS_SDN_CHECK_DATE_RANGES should fulfil your requirement:



*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_CHECK_DATE_RANGES
*&
*&---------------------------------------------------------------------*
*& Thread: to delete records whose dates are not overlapping
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1157341"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_check_date_ranges.




TYPES: BEGIN OF ty_s_data.
TYPES:   from   TYPE begda.
TYPES:   to     TYPE endda.
TYPES: END OF ty_s_data.
TYPES: ty_t_data    TYPE STANDARD TABLE OF ty_s_data
                    WITH DEFAULT KEY.


DATA: gt_data   TYPE ty_t_data,
      gs_data   TYPE ty_s_data,
      gd_idx    TYPE i.


DEFINE mac_add_date.
  gs_data-from = &1.
  gs_data-to   = &2.
  append gs_data to gt_data.
END-OF-DEFINITION.

DEFINE mac_write_list.
  loop at &1 into gs_data.
    write:/ 'Date=', gs_data-from, gs_data-to.
  endloop.
  skip.
END-OF-DEFINITION.


START-OF-SELECTION.

**  7.7.2008 8.7.2008
**  7.7.2008 7.7.2008
**  9.7.2008 10.7.2008 is to be deleted(not overlapping
**                        with other dates)
**  8.7.2008 8.7.2008
**  11.7.2008 13.7.2008
**  12.7.2008 14.7.2008

  REFRESH: gt_data.
  mac_add_date: '20080707' '20080708',
                '20080707' '20080707',
                '20080709' '20080710',
                '20080708' '20080708',
                '20080711' '20080713',
                '20080712' '20080714'.

  mac_write_list gt_data.

  BREAK-POINT.


  LOOP AT gt_data INTO gs_data.
    gd_idx = syst-tabix.

    LOOP AT gt_data TRANSPORTING NO FIELDS
            WHERE NOT ( from = gs_data-from AND to = gs_data-to )
            AND   ( ( from BETWEEN gs_data-from AND gs_data-to )
                          OR
                    ( to   BETWEEN gs_data-from AND gs_data-to ) ).
      EXIT.
    ENDLOOP.
    IF ( sy-subrc NE 0 ).
      DELETE gt_data INDEX gd_idx.
    ENDIF.

  ENDLOOP.

  mac_write_list gt_data.

END-OF-SELECTION.

Regards

Uwe

Hi All,

I have an internal table.

two of the fields are

FROM TO

7.7.2008 8.7.2008

7.7.2008 7.7.2008

9.7.2008 10.7.2008 is to be deleted(not overlapping with other dates)

8.7.2008 8.7.2008

11.7.2008 13.7.2008

12.7.2008 14.7.2008

Now I have to delete the records whose dates do not overlap.

i.e each and every record has to be compared with each each and every other record.

As per the above data, my final internal table should have

FROM TO

7.7.2008 8.7.2008

7.7.2008 7.7.2008

8.7.2008 8.7.2008

11.7.2008 13.7.2008

12.7.2008 14.7.2008

How do you achieve it?

Thanks,

Sandeep.

7 REPLIES 7
Read only

Former Member
0 Likes
1,300
loop at itab into watab.

read table itab into watab1 with key from >= watab-from
                                                    to     <= watab-to.
if sy-subrc ne 0.
delete watab from itab.
endif.

endloop.
Read only

0 Likes
1,300

Hi Prashant,

Will this logic compare all the records with all the other records?

i.e if I have 10 records, each record should be checked with all the other 9 records.

Thanks,

Sandeep.

Read only

Former Member
0 Likes
1,300

Yes sandeep it will scan all the records.

Best regards,

Prashant

Read only

Former Member
0 Likes
1,300

Hi,

You can use 'ranges' and compare to delete.

-Maharshi

Read only

Former Member
0 Likes
1,300

Hi,

so i think

11.7.2008 13.7.2008

12.7.2008 14.7.2008, these two should alos be deleted!!

try to do like this:

data:temp_tab type......."Declare a temp internal table which will hold the FROM and TO dates with fields "DATE" and a "FLAG" to hold whether its an overlapping entry or not.

SORT i_tab with from to.

loop at I_tab into wa_tab.
if temp_tab is initial.
     move wa_iab-from to wa_temp-date.
     append wa_temp to temp_tab.
     move wa_iab-to to wa_temp-date.
     append wa_temp to temp_tab.
else.
     read table temp_tab into wa_temp with key date = wa_itab-from.
     if sy-subrc = 0.
          wa_temp-flag = 'X'
          modify table temp_tab from wa_temp transporting flag.
    else.
           move wa_iab-from to wa_temp-date.
               append wa_temp to temp_tab.
     endif.
     read table temp_tab into wa_temp with key date = wa_itab-to.
         if sy-subrc = 0.
              wa_temp-flag = 'X'
              modify table temp_tab from wa_temp transporting flag.
          else.
              move wa_iab-to to wa_temp-date.
               append wa_temp to temp_tab.
           endif.
   endif.
endloop.
loop at temp_tab into wa_temp.
    if wa_temp-flag is initial.
          ==>delete the entries from i_tab where FLAG is initial.
    endif.
endloop.

Edited by: Neha Shukla on Dec 8, 2008 11:35 AM

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,301

Hello Sandeep

Assuming that there are no duplicated records in your dates itab then sample report ZUS_SDN_CHECK_DATE_RANGES should fulfil your requirement:



*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_CHECK_DATE_RANGES
*&
*&---------------------------------------------------------------------*
*& Thread: to delete records whose dates are not overlapping
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1157341"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_check_date_ranges.




TYPES: BEGIN OF ty_s_data.
TYPES:   from   TYPE begda.
TYPES:   to     TYPE endda.
TYPES: END OF ty_s_data.
TYPES: ty_t_data    TYPE STANDARD TABLE OF ty_s_data
                    WITH DEFAULT KEY.


DATA: gt_data   TYPE ty_t_data,
      gs_data   TYPE ty_s_data,
      gd_idx    TYPE i.


DEFINE mac_add_date.
  gs_data-from = &1.
  gs_data-to   = &2.
  append gs_data to gt_data.
END-OF-DEFINITION.

DEFINE mac_write_list.
  loop at &1 into gs_data.
    write:/ 'Date=', gs_data-from, gs_data-to.
  endloop.
  skip.
END-OF-DEFINITION.


START-OF-SELECTION.

**  7.7.2008 8.7.2008
**  7.7.2008 7.7.2008
**  9.7.2008 10.7.2008 is to be deleted(not overlapping
**                        with other dates)
**  8.7.2008 8.7.2008
**  11.7.2008 13.7.2008
**  12.7.2008 14.7.2008

  REFRESH: gt_data.
  mac_add_date: '20080707' '20080708',
                '20080707' '20080707',
                '20080709' '20080710',
                '20080708' '20080708',
                '20080711' '20080713',
                '20080712' '20080714'.

  mac_write_list gt_data.

  BREAK-POINT.


  LOOP AT gt_data INTO gs_data.
    gd_idx = syst-tabix.

    LOOP AT gt_data TRANSPORTING NO FIELDS
            WHERE NOT ( from = gs_data-from AND to = gs_data-to )
            AND   ( ( from BETWEEN gs_data-from AND gs_data-to )
                          OR
                    ( to   BETWEEN gs_data-from AND gs_data-to ) ).
      EXIT.
    ENDLOOP.
    IF ( sy-subrc NE 0 ).
      DELETE gt_data INDEX gd_idx.
    ENDIF.

  ENDLOOP.

  mac_write_list gt_data.

END-OF-SELECTION.

Regards

Uwe

Read only

0 Likes
1,300

Hi Uwe Schieferstein,

Thank you soo much for your code.

Its working fine.

Regards,

Sandeep.