Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count itab rows that meet some condition?

I get a internal table from a Function Module call that returns ~ 100 rows. About 40% of the rows are not relevant to me because I only need the entries with PAR1 = "XYZ". On SQL tables (transparent tables), I can use a

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 to iterate through the table. My current approach is to basically have a loop and increase if the row contains the value I want. But this seems very inefficient.

Is there a better approach for my requirement?

like image 783
Jasper Avatar asked Aug 05 '13 14:08

Jasper


People also ask

What is the use of reduce operator in SAP ABAP?

REDUCE - Reduction Operator - ABAP Keyword Documentation. A constructor expression with the reduction operator REDUCE creates a result of a data type specified using type from one or more iteration expressions. The following can be specified for type: A non-generic data type dtype.

What is the use of describe table in SAP ABAP?

The statement DESCRIBE is used to determine the attributes of data objects of elementary data types. When DESCRIBE is used for structures or data objects of deep data types like strings, internal tables, or reference variables, only elementary attributes can be determined.


3 Answers

As from 740 SP05 you can use:

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

for counting the number of lines in gt_itab meeting codntion f1 = 'xyz'.

like image 167
Mehmet Metin Avatar answered Sep 21 '22 08:09

Mehmet Metin


Do whatever feels right to you. With ~100 rows, virtually nothing will make a huge difference in runtime. For me, stability would be more important than speed in this case.

That being said, you could try this:

LOOP AT lt_my_table TRANSPORTING NO FIELDS WHERE par1 = 'XYZ'.
  ADD 1 TO l_my_counter.
ENDLOOP.
like image 24
vwegert Avatar answered Sep 21 '22 08:09

vwegert


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

DELETE lt_table WHERE par1 <> 'XYZ'.

Then you can count the remaining relevant records by 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.
like image 22
Jagger Avatar answered Sep 20 '22 08:09

Jagger