08-23-2010 10:07 AM
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?
08-23-2010 10:20 AM
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
08-23-2010 10:14 AM
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
08-23-2010 10:20 AM
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
08-23-2010 10:20 AM
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
08-23-2010 10:38 AM
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.
08-24-2010 9:07 AM
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
08-24-2010 1:35 PM
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
08-24-2010 1:56 PM
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.
09-07-2010 7:03 AM
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.
11-30-2010 12:18 PM
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.
12-08-2010 4:13 AM
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.
12-08-2010 2:20 PM
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
12-09-2010 8:12 AM
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 ......
01-21-2011 8:33 AM
01-24-2011 4:55 PM
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
01-24-2011 6:56 PM
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.
01-24-2011 7:04 PM
Before anyone else answers, note the date of the original post
Rob
03-23-2011 7:49 AM
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
03-23-2011 11:18 AM
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.