Application Development 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: 

Get records number from internal table with condition.

Former Member
0 Kudos

Internal table itab got more than 1000 records,now i need to get the number of records with condition that itab-field1 = 'XXXX'.

actully, i got an inefficient logic to count the number in a loop statement. is there better way to implement it?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Dear,

Copy all the records of internal table(itab1) to another internal table(itab2).

Delete the records from internal table(itab2) where field1 NE 'XXXX'.

Now use the statement Describe table to know the no of records in itab2.

If any doubt, please revert me.

Regards

DKS

18 REPLIES 18

birendra_chatterjee
Active Participant
0 Kudos

Hi,

Declare temporary internal table 'itab1' similar to 'itab'. Then implement the following code.

data: w_lines type i.

append lines of itab to itab1.

delete itab1[] where field1 ne 'XXXX'.

describe table itab1 lines w_lines.

free itab1.

Regards,

Birendra

ThomasZloch
Active Contributor
0 Kudos

1000 records is not that many, I can't see what should be inefficient about looping here. Or does this happen inside an outer loop?

Try optimizing it by using a sorted table (declared as such, not using the SORT statement), sorted by the field(s) as they appear in the where-condition.

I don't think that duplicating the data then deleting from the copy is more efficient, maybe you want to compare the two options and post the result?

Thomas

Former Member
0 Kudos

Dear,

Copy all the records of internal table(itab1) to another internal table(itab2).

Delete the records from internal table(itab2) where field1 NE 'XXXX'.

Now use the statement Describe table to know the no of records in itab2.

If any doubt, please revert me.

Regards

DKS

Former Member
0 Kudos

Why don't you try counting the number of records for your condition wherever you populate this internal table.

That would save your extra effort and won't hamper you code in anyway

Regards,

Lalit Mohan Gupta.

former_member131774
Contributor
0 Kudos

I think you can just count the number with condition with loop statement

Copy and delete records will cost extra memory consumption also not efficient.

Also 1000 records is not huge number... you should not worry too much

Subhankar
Active Contributor
0 Kudos

hello,

Every time assigning data into temp table and delete may that may not be much efficient. You can check in loop logic how much time is taken. You can check the below code. here I am trying to check the numbers of plant for for one material.

In this logic material is the first field. So if there is option to make the required field in to the 1 st position then it will be nice.

TYPES: BEGIN OF x_count,

matnr TYPE matnr,

count TYPE i,

BEGIN OF x_count.

DATA: i_marc TYPE STANDARD TABLE OF marc,

i_count TYPE STANDARD TABLE OF x_count,

wa_count TYPE x_count.

FIELD-SYMBOLS: <wa_marc> TYPE marc.

SELECT * UP TO 1000 ROWS

FROM marc

INTO TABLE i_marc.

IF sy-subrc = 0.

SORT i_marc BY matnr.

LOOP AT i_marc ASSIGNING <wa_marc>.

wa_count-count = wa_count-count + 1.

AT END OF matnr.

wa_count-matnr = <wa_marc>-matnr.

APPEND wa_count TO i_count.

ENDAT.

ENDLOOP.

ENDIF.

Thanks

Subhanakr

0 Kudos

hi,

just LOOP on the internal with WHERE clause to get the record count. Don't copy data into another internal table and use DELETE statement, because DELETE with WHERE clause has to read the entire table for deleting the records.

Former Member
0 Kudos

What i suggest is that you put all the data in a sorted internal table and put the where condition during the time you are putting data into the internal tables. This will minimise the access time. Then use the statements DESCRIBE TABLE, LOOP AT, and READ TABLE. Finally SY-TFILL will contain the number of lines of the relevant internal table.

himanshu_gupta2
Participant
0 Kudos

Hi,

You can do the following steps and check the performance.

lw_count1 and lw_count 2 .

1) Sort the table by field1.

2) loop at table

3) at end "field1

lw_count 1 = lw_count 2.

lw_count = sy-tabix.

end at.

4) if field 1 == 'xxx'

break the loop..

5) total records = lw_count 2 - lw_count1.

I think it will save more time.

Please correct me if i wrong.

ravi_lanjewar
Contributor
0 Kudos

Hi,

1) Define sorted table with non-unique key on the condition which you want to count record.

2) Read data which you want from database to sorted internal tables or assign data to sorted table from other table but keep data to be sorted as define non-unique key.

3) loop using transporting no fields and count the record or increment counter inside the loop.


loop at abc TRANSPORTING NO FIELDS WHERE a = '1'.
  count = count + 1.
endloop.   

You can also used fields symbols but it is also required assign refrence to fields symbol.

TRANSPORTING NO FIELDS seems to be give better performance over fields symbols.

former_member194613
Active Contributor
0 Kudos

as Thomas said, 1000 is rather small, so do something, we talking about microseconds!

But please note


data: w_lines type i.
append lines of itab to itab1.
delete itab1[] where field1 ne 'XXXX'.
describe table itab1 lines w_lines.
free itab1.

this is probably the most inefficient way I could think about.

You loop and count, and maybe if the table is width then you can use TRANSPORTING NO FIELDS.

never SORT, never MOVE, never DELETE.

Siegfried

anjaneya_bhardwaj2
Contributor
0 Kudos

for your no of records looping at source internal table itself should be sufficient ....and describe table later .....moving th data to other internal table have other costs with it ......

0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

DESCRIBE TABLE <itab-Name> LINES <variable>

After the call, variable contains the number of rows of the internal table .

I think it will helpful for you

0 Kudos

DESCRIBE TABLE should not be used any more. Use the following:


number_of_lines = LINES( <internal table name> ).

You can do something like:


IF LINES( <itab> ) > 10.
* Do something
ENDIF.

0 Kudos

Before anyone else answers, note the date of the original post

Rob

Former Member
0 Kudos

Hi,

Try with ' Find all the occurences of ' statement as below.

FIND ALL OCCURRENCES OF fieldname-value IN TABLE itab MATCH COUNT v_cnt RESPECTING CASE .

where v_cnt is an integer which will return the number of times the values present in the table

Regards

Chellamma Chandrasekar

Former Member
0 Kudos

Hi

Describe Statement is slower than, lines(internal table).

DATA : v_line TYPE i.

DATA : t_tab TYPE TABLE OF sflight.

SELECT * INTO TABLE t_tab FROM sflight.

v_line = LINES( t_tab ).

WRITE : / v_line.