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

Delete internal table with condition and same ID

Former Member
0 Likes
2,749

Hello experts,

I have a problem I can not solve, and hope you can help me.

Here is an internal table:

ID name Datefrom

1 Test 02.10.2018

1 Test1 01.10.2018

2 Test3 29.09.2018

The rows with the same ID belong together. I have a report where you can select the dates. If you choose for example the date „02.10.2018“. The result should show up both rows with the same id . This is because at least one of the rows with the same id meets the condition.

For example if I put „02.10.2018“ in the parameter field, I want the result to show to this example below:

ID name Datefrom

1 Test 02.10.2018

1 Test1 01.10.2018

Here is what I tried so far and I don't get any results.

FIELD-SYMBOLS <wa_itab> LIKE LINE OF itab. LOOP AT itab ASSIGNING <wa_itab>.
IF <wa_itab>date < p_date.
DELETE TABLE itab FROM <wa_itab>ENDIF.
ENDLOOP.

I am very thankful for any help.

1 ACCEPTED SOLUTION
Read only

arpan_shukla
Explorer
1,928

Dear Zeynep,

If you need entries specific to one parameter. You can do it without using loop and simply with one statement.

DELETE itab WHERE date NE p_date.

I hope this will resolve your issue.

Thanks and Regards,

Arpan Shukla

3 REPLIES 3
Read only

arpan_shukla
Explorer
1,929

Dear Zeynep,

If you need entries specific to one parameter. You can do it without using loop and simply with one statement.

DELETE itab WHERE date NE p_date.

I hope this will resolve your issue.

Thanks and Regards,

Arpan Shukla

Read only

MichaelTe
Contributor
0 Likes
1,928

*&---------------------------------------------------------------------*
*& Report ZWELT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zwelt.


 PARAMETERS: p_date TYPE sy-datum DEFAULT '20181002'.

 TYPES BEGIN OF it_tabline.
   TYPES: id TYPE i,
          name(10) TYPE c,
          date TYPE sy-datum.
   TYPES END OF it_tabline.
 DATA: it_tab TYPE TABLE OF it_tabline.
 DATA: lr_tab TYPE it_tabline.

 FIELD-SYMBOLS <wa_itab> LIKE LINE OF it_tab.

 lr_tab-id = 1.
 lr_tab-name = 'Test'.
 lr_tab-date = '20181002'.
 APPEND lr_tab TO it_tab.

 lr_tab-id = 1.
 lr_tab-name = 'Test1'.
 lr_tab-date = '20181001'.
 APPEND lr_tab TO it_tab.

 lr_tab-id = 2.
 lr_tab-name = 'Test3'.
 lr_tab-date = '20180929'.
 APPEND lr_tab TO it_tab.

 READ TABLE it_tab INTO lr_tab WITH KEY date = p_date.

 CHECK sy-subrc EQ 0.

 LOOP AT it_tab ASSIGNING <wa_itab> WHERE id EQ lr_tab-id.
   WRITE: / <wa_itab>-id,
            <wa_itab>-name,
            <wa_itab>-date.
 ENDLOOP.

or if you want to delete the lines with the same id: put instead of the loop. endloop.:

 DELETE it_tab WHERE id EQ lr_tab-id.

Regards,

Michael

Read only

Former Member
0 Likes
1,928

Dear Zeynep,

even if your request is already old, maybe my solution (works only with NW7.40PL08 and higher) helps you in future.

REPORT zstkoes_internal_table_new.


TYPES:
  BEGIN OF gtys_data,
    id   TYPE i,
    name TYPE name1,
    date TYPE d,
  END OF gtys_data,
  gtyt_data TYPE TABLE OF gtys_data WITH DEFAULT KEY.


PARAMETERS: pa_date TYPE d DEFAULT '20181002'.


DATA(gt_data) = VALUE gtyt_data(
                  ( id = 1 name = 'Test'  date = '20181002' )
                  ( id = 1 name = 'Test1' date = '20181002' )
                  ( id = 1 name = 'Test2' date = '20181001' )
                  ( id = 2 name = 'Test3' date = '20180929' ) ).


cl_demo_output=>begin_section( |Original Data| ).
cl_demo_output=>write_data( gt_data  ).


DATA(gt_data_nested_loop) = VALUE gtyt_data(  FOR ls_data IN gt_data
                                                WHERE ( date EQ pa_date )   " First loop with valid parameter-date
                                              FOR ls_data_filtered IN gt_data
                                                WHERE ( id EQ ls_data-id )  " Second loop for all ids
                                                  ( ls_data_filtered ) ).   " Entries which fits both condition


cl_demo_output=>begin_section( |Nested Loop| ).
cl_demo_output=>write( |Be careful with this logic.{
                        cl_abap_char_utilities=>newline }It can cause multiple entries, if date condition one id fits multiple times.{
                        cl_abap_char_utilities=>newline }If date is unique then logic is working fine.| ).
cl_demo_output=>write_data( gt_data_nested_loop ).


DATA(gt_data_grouped_loop) = VALUE gtyt_data( FOR GROUPS ids OF ls_data IN gt_data
                                                WHERE ( date EQ pa_date )   " First loop to get ids of valid parameter-date
                                                GROUP BY ls_data-id
                                              FOR ls_data_filtered IN gt_data
                                                WHERE ( id EQ ids )         " Second loop for all ids
                                                  ( ls_data_filtered ) ).   " Entries which fits both condition
cl_demo_output=>begin_section( |Loop Grouped by| ).
cl_demo_output=>write( |This logic is working perfect.| ).
cl_demo_output=>write_data( gt_data_grouped_loop ).




cl_demo_output=>display( ).

And here is the output of the report:

Greetings
Stephan