2010 Jan 05 4:45 AM
How do i count the number of records in an internal table without using the loop statement.
The Describe statement give the total no. of records in the internal table.But i want the count of the key fields in the internal table
For eg
Row Field
1 A
2 A
3 A
4 B
5 B
6 C
Count of A = 3
Count of B = 2
Count of C = 1
2010 Jan 05 4:01 PM
The key fields of a internal table are the non numeric fields.
Its not the key field count.
it must be count based on unique data in internal table.
Without a loop its not possible because its a manipulation with the number of records.
types:begin of ty,
field1 type c,
row type i.
end of ty.
data:it type table of ty,
wa type ty.
loop at itab into wa1. "itab holds your data
wa-field1 = wa1-field1 "Here 'A' for ex.
wa-row = 1.
collect wa into it.
clear wa.
endloop.
2010 Jan 05 3:44 PM
If you want number of key fields in the internal table which you will get from the inetral table defination
ie suppose if mara
it_mara type standard table of mara..
so if you can check the mara table you will get the number of key fields
Regards
Satish Boguda
2010 Jan 05 4:01 PM
The key fields of a internal table are the non numeric fields.
Its not the key field count.
it must be count based on unique data in internal table.
Without a loop its not possible because its a manipulation with the number of records.
types:begin of ty,
field1 type c,
row type i.
end of ty.
data:it type table of ty,
wa type ty.
loop at itab into wa1. "itab holds your data
wa-field1 = wa1-field1 "Here 'A' for ex.
wa-row = 1.
collect wa into it.
clear wa.
endloop.
2013 Jul 19 12:13 PM
2010 Jan 05 4:08 PM
Dilip,
I think isn't posible. Only 3 ideas:
1ª, make other table "table2", similar to your actual table.
table2[] = yourtable[].
delete table2 where key <> 'A'.
describe table table2 lines contA.
But depending the amount of data, you may have problems in performance or memory consumption...
2ª, make other table, only with the keys fields and a count field,
data begin table2
key_field1,
...
cont type i.
end data.
when you append lines to yourdata, make a collect to this table:
table2-keys = yourtable-keys.
table2-cont = 1.
collect table2.
But only works if you can modify the program where data is appended, and you may be carefull when data is deleted :-(...
3ª if your data if filled in a select, you may fill the table2 of point 2ª, making a similar select but:
select keyfields count( * ) into table table2
from ...
where (the same you have to fill your original table...)
group by keys.
But you have the same problem is data is deleted later...
I hope that any of the three is useful for you...
Edited by: Diego Alvarez on Jan 5, 2010 5:09 PM
2010 Jan 05 4:10 PM
You can use the READ or DO statements instead, but it won't help much if at all. One way or another, each record has to be checked.
Rob
2010 Jan 05 8:03 PM
Hi Dilip,
I think u should go for collect statment.
Like
if ur taking data from DB then use collect stat between select and endselect.
If ur appending the data instead of append write collect.
It will give u count for ur default char field.
Then display this with the help of Loop and endloop.
U have to use loop and endloop atleast for displying.
Thanks and Regards,
Anmol
2010 Jan 05 9:57 PM
Hi Dilip,
It appears you will have to use one or the other type of loop (do/while/loop) to get your result. I do not see any other go.
However Iu2019d like to know what is your exact requirement so that we may find any other option which is not very obvious with the given example.
Thanks,
Pritam
2013 Jul 20 7:18 AM
2013 Jul 21 10:27 PM
Use collect but ensure you have characters and numeric fields in your internal table.
you can preparing an internal table with two fields as shown below by moving details from the first table using collect statement.
Char Num
Field 1 Field 2
A 1
A 1
A 1
B 1
B 1
Thanks
2013 Jul 22 8:09 AM
Hi Dilip,
For your scenario, you can use Group by in Query for this, if have data into more than one table you can also use sub-query. I think there is no option to count group by from internal table.
Ram Shanker
2019 Jul 23 9:07 AM
Hello all,
Here is some code to calculate sum of a field in an internal table without LOOP...ENDLOOP
select * from mard into table @data(lta_data).
select sum( a~labst ) from @lta_data as a into @data(lv_sum).
write lv_sum.
NOTE:- This code has been successfully executed in ABAP 7.4 version, and this feature has to be supported by your database.
Even though it's been long since this question has posted. we just want to share. it may help some one!!
Regards,
ABAP Newbies
2019 Jul 23 2:57 PM
Why not using the shorter code below? (it used to work for last 20 years in all the ABAP versions I used)
select sum( labst ) from mard into lv_sum.
2019 Jul 25 2:50 AM
Hi Sandra, I can't speak for Srikar, but I think the point is to show the SELECT from internal table as a data source. Very useful if you have data coming from a file.
2019 Jul 25 6:21 AM
Custodio de Oliveira Thanks. I realize now what the question was (counting the number of distinct values of a column in a internal table) and I just looked at the answer's code without the context and felt that the proposed code was non-sense (and now I see that it used SUM instead of COUNT GROUP BY if it really wants to answer the question). But beware of performance drop versus classic ABAP: for the simple aggregation "sum( labst )" I did a test of SELECT FROM @itab versus REDUCE which gives REDUCE: 2566.29 < SELECT FROM @itab: 3362.21. Well I must say I'm impressed by SELECT, it's only 35% slower for this simple query, the compiler seems good, but it might be worse for more complex SELECT queries.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |