Write records in internal abap table based on query

I get the internal table from a FUBA call having ~ 100 rows. About 40% of the lines do not apply to me, because I only need records with PAR1 "XYZ". In SQL tables (transparent tables) I can use

select count(*) from tab where PAR1 = "XYZ" 

to get the number of valid entries.

Looking at the documentation, all I could find was the READ Table syntax for iterating over a table. My current approach is to basically have a loop and increment if the row contains the value I want. But it seems very inefficient.

Is there a better approach for my requirement?

+4
source share
3 answers

Do whatever you want. With ~ 100 rows, virtually nothing will matter much at runtime. For me, stability in this case would be more important than speed.

In doing so, you can try the following:

 LOOP AT lt_my_table TRANSPORTING NO FIELDS WHERE par1 = 'XYZ'. ADD 1 TO l_my_counter. ENDLOOP. 
+5
source

If the entries in the internal table are irrelevant, you can do something like this.

 DELETE lt_table WHERE par1 <> 'XYZ'. 

You can then count the remaining matching records using lines( lt_table ) or DESCRIBE TABLE lt_table LINES l_number_of_lines.

Here is an example.

 TYPES: BEGIN OF tt_test, par1 TYPE c LENGTH 3, END OF tt_test. DATA: lt_table TYPE TABLE OF tt_test. DATA: l_number_of_lines TYPE i. FIELD-SYMBOLS: <fs_par1> LIKE LINE OF lt_table. APPEND INITIAL LINE TO lt_table ASSIGNING <fs_par1>. <fs_par1>-par1 = 'XYZ'. APPEND INITIAL LINE TO lt_table ASSIGNING <fs_par1>. <fs_par1>-par1 = 'ABC'. APPEND INITIAL LINE TO lt_table ASSIGNING <fs_par1>. <fs_par1>-par1 = 'XYY'. APPEND INITIAL LINE TO lt_table ASSIGNING <fs_par1>. <fs_par1>-par1 = 'XYZ'. APPEND INITIAL LINE TO lt_table ASSIGNING <fs_par1>. <fs_par1>-par1 = 'XYZ'. l_number_of_lines = LINES( lt_table ). WRITE / l_number_of_lines. DESCRIBE TABLE lt_table LINES l_number_of_lines. WRITE / l_number_of_lines. DELETE lt_table WHERE par1 <> 'XYZ'. l_number_of_lines = LINES( lt_table ). WRITE / l_number_of_lines. 
+6
source

Starting at 740 you can use:

 DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa IN gt_itab WHERE( F1 = 'XYZ' ) NEXT x = x + 1 ). 

to count the number of lines in gt_itab that match the code name f1 = 'xyz'.

+3
source

Source: https://habr.com/ru/post/1495337/


All Articles