Count itab rows that meet some condition?

Jasper picture Jasper · Aug 5, 2013 · Viewed 24.3k times · Source

I get a internal table from a FUBA call that has ~ 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?

Answer

Mehmet Metin picture Mehmet Metin · Aug 11, 2017

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'.