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

Internal Table Counting

Former Member
0 Likes
819

Hello everyone,

I want to be able to SELECT COUNT(*) from an internal table where a certain

condition exists i.e. COL1 = 'A' like you would do with a normal database table.

Does anyone have any tricks up their sleeves to do this???

Andy

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
574

Hi

If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself. Some of the Aggregate functions allowed in SAP are MAX, MIN, AVG, SUM, COUNT, COUNT( * )

Consider the following extract.

            Maxno = 0.

            Select * from zflight where airln = ‘LF’ and cntry = ‘IN’.

             Check zflight-fligh > maxno.

             Maxno = zflight-fligh.

            Endselect.

The above mentioned code can be much more optimized by using the following code.

Select max( fligh ) from zflight into maxno where airln = ‘LF’ and cntry = ‘IN’.

If you want to count number of records then

LV_COUNT = 0.

LOOP AT IT_OUTPUT INTO WA_OUTPUT.

LV_COUNT = LV_COUNT + 1.

WA_OUTPUT-COUNT = LV_COUNT.

APPEND WA_OUTPUT TO IT_OUTPUT_1.

ENDLOOP.

REFRESH IT_OUTPUT.

IT_OUTPUT = IT_OUTPUT_1.

reward if usefull

4 REPLIES 4
Read only

Former Member
0 Likes
575

Hi

If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself. Some of the Aggregate functions allowed in SAP are MAX, MIN, AVG, SUM, COUNT, COUNT( * )

Consider the following extract.

            Maxno = 0.

            Select * from zflight where airln = ‘LF’ and cntry = ‘IN’.

             Check zflight-fligh > maxno.

             Maxno = zflight-fligh.

            Endselect.

The above mentioned code can be much more optimized by using the following code.

Select max( fligh ) from zflight into maxno where airln = ‘LF’ and cntry = ‘IN’.

If you want to count number of records then

LV_COUNT = 0.

LOOP AT IT_OUTPUT INTO WA_OUTPUT.

LV_COUNT = LV_COUNT + 1.

WA_OUTPUT-COUNT = LV_COUNT.

APPEND WA_OUTPUT TO IT_OUTPUT_1.

ENDLOOP.

REFRESH IT_OUTPUT.

IT_OUTPUT = IT_OUTPUT_1.

reward if usefull

Read only

amit_khare
Active Contributor
0 Likes
574

We cannot directly have the count.

Check these options -

You cannot find the count in one direct statement. You will have to use the count() in your select, or if that is not possible, then:

Easy option:

loop at itab where field1 = condition1.

counter = counter + 1.

endloop.

A lesser easy way, and not always feasible:

itab_copy[] = itab[].

delete itab_copy where field1 NE condition1.

describe table itab_copy lines lv_lines.

~ As found in forum

Regards,

Amit

Reward all helpful replies.

Read only

Former Member
Read only

Former Member
0 Likes
574

Hi,

Use

loop at itab where field1 = condition1 (e.g col1 = 'A').

counter = counter + 1.

endloop.

If it is helpful pls rewards points.

Regards

Srimanta