Learn how to count matching rows in a SAP ABAP internal table with REDUCE, LOOP AT ... WHERE, lines( ), GROUP BY, and SELECT COUNT(*).

Quick answer: count all or matching rows
Use lines( itab ) to count every row in an internal table. For a conditional count, use LOOP AT ... WHERE for broad compatibility or REDUCE for a modern expression-based solution.
DATA( total_rows ) = lines( itab ). DATA( matching_rows ) = REDUCE i( INIT count = 0 FOR row IN itab WHERE ( status = 'A' ) NEXT count = count + 1 ).
The best method depends on whether the table is already loaded, whether a condition is required, and which ABAP release the target system supports.
Count matching rows with REDUCE
REDUCE evaluates a FOR expression and accumulates a result. The INIT value starts the counter, the WHERE condition filters rows, and NEXT increments the result.
Definition of REDUCE:
... REDUCE type(
LET expression (optional)
INIT ||
||
...
FOR for_exp1
FOR for_exp2
...
NEXT ...
|
|
... ) ...
While and expressions can include expressions, must include at least one FOR expression. You can use all kinds of expressions in :
- with
for iterating internal tables - with
orfor conditional iterations
Source: https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenconstructor_expression_reduce.htm
Classic LOOP AT … WHERE
DATA: lv_lines TYPE i. LOOP AT gt_itab INTO ls_itab where F1 = 'XYZ'. lv_lines = lv_lines + 1. ENDLOOP.
Modern REDUCE syntax
Constructor expressions such as REDUCE are available on sufficiently modern ABAP releases. Check the target ABAP platform and support package before transporting this syntax.
DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa IN gt_itab
WHERE( F1 = 'XYZ' ) NEXT x = x + 1 ).
Source: https://blogs.sap.com/2015/10/25/abap-740-quick-reference/
Runnable REDUCE example
This self-contained example counts rows whose status is active and returns 2.
TYPES: BEGIN OF ty_item,
id TYPE i,
status TYPE c LENGTH 1,
END OF ty_item.
DATA(lt_items) = VALUE STANDARD TABLE OF ty_item(
( id = 1 status = 'A' )
( id = 2 status = 'I' )
( id = 3 status = 'A' )
).
DATA(lv_active_count) = REDUCE i(
INIT count = 0
FOR item IN lt_items
WHERE ( status = 'A' )
NEXT count = count + 1
).
Count all rows with lines( )
lines( itab ) counts every row and does not apply a filter. The classic equivalent is DESCRIBE TABLE ... LINES.
DATA(lv_total) = lines( gt_itab ). DATA lv_total_classic TYPE i. DESCRIBE TABLE gt_itab LINES lv_total_classic.
Count matching rows with LOOP AT
The classic loop is easy to debug and is often the safest option for older ABAP systems.
DATA lv_count TYPE i.
LOOP AT gt_itab INTO DATA(ls_itab)
WHERE f1 = 'XYZ'.
lv_count = lv_count + 1.
ENDLOOP.
Count filtered rows with VALUE or FILTER #( )
TYPES tt_mara TYPE STANDARD TABLE OF mara WITH EMPTY KEY.
DATA(lv_count) = lines(
VALUE tt_mara(
FOR line IN lt_mara
WHERE ( matnr = 'XXX' )
( line )
)
).
This creates a filtered value table before counting it. For large tables, REDUCE or LOOP AT ... WHERE may avoid the temporary table.
Count grouped rows with GROUP BY
Aggregate rows with COLLECT
COLLECT combines rows according to the internal table key and adds numeric fields. It is useful for aggregation, but it is not the simplest general-purpose row-counting statement.
DATA: BEGIN OF seats,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
seatsocc TYPE sflight-seatsocc,
END OF seats.
DATA seats_tab LIKE HASHED TABLE OF seats
WITH UNIQUE KEY carrid connid.
SELECT carrid, connid, seatsocc
FROM sflight
INTO @seats.
COLLECT seats INTO seats_tab.
ENDSELECT.
Count rows in the database with SELECT COUNT(*)
Use database counting when the rows are still in the database and do not need to be loaded into an internal table. Avoid selecting unnecessary columns just to count records.
SELECT COUNT( * ) FROM mara WHERE matkl = @lv_material_group INTO @DATA(lv_database_count).
Choose the right ABAP counting method
| Requirement | Recommended construct |
|---|---|
| Count every row | lines( itab ) |
| Count rows with a condition | REDUCE or LOOP AT ... WHERE |
| Support older ABAP releases | LOOP AT ... WHERE |
| Count rows per group | GROUP BY ... GROUP SIZE |
| Count rows in the database | SELECT COUNT( * ) |
| Aggregate numeric values by key | COLLECT |
ABAP release and performance considerations
lines( ) and LOOP AT ... WHERE are suitable for many systems. Verify that the target ABAP release supports constructor expressions before using REDUCE, FILTER #( ), or newer grouping syntax. When only a count is required and the data is still in the database, prefer SELECT COUNT( * ) instead of loading every row into an internal table. For large in-memory tables, consider the table category, keys, and whether a secondary key can avoid an unnecessary full scan.
LOOP AT ... WHEREis the broad-compatibility option.- Constructor expressions such as
REDUCE,VALUE, andFILTERrequire a sufficiently modern ABAP release and support package. - Use
SELECT COUNT( * )when data is still in the database and no row data is needed in ABAP. - Use
lines( )when the internal table is already populated. - For large tables, consider the table type and available sorted or hashed keys before creating a filtered copy.
Frequently asked questions
Can lines( ) count only rows matching a condition?
No. lines( itab ) returns the total number of rows in the table. Use LOOP AT ... WHERE, REDUCE, or FILTER #( ) when the count must satisfy a condition.
When should I use FILTER #( ) instead of REDUCE?
Use FILTER #( ) when you need the matching rows as a new internal table. Use REDUCE when you need an aggregate such as a count without creating a second table.
Why use SELECT COUNT( * ) instead of counting an internal table?
Use SELECT COUNT( * ) when the rows are still in the database and only the count is needed. This avoids transferring and storing rows that ABAP does not need.
How do I count rows in an ABAP internal table?
Use lines( itab ) for all rows, or use REDUCE and LOOP AT ... WHERE when a condition is required.
What is the difference between lines( ) and DESCRIBE TABLE?
Both can return the total row count. lines( ) is concise modern syntax, while DESCRIBE TABLE ... LINES is the classic form.
Which method works on older ABAP systems?
LOOP AT ... WHERE is generally the safest compatibility option. Verify newer constructor expressions on the target ABAP release.
Should I use SELECT COUNT(*) or count an internal table?
Use SELECT COUNT( * ) when the data is still in the database. Use ABAP expressions when the internal table has already been loaded.
Is COLLECT the same as counting rows?
No. COLLECT aggregates rows according to the internal table key and adds numeric fields; it is not simply a row-counting statement.
