05-17-2016 5:56 PM
Hi Experts, I have an internal table holding various types of product. The number of entries in this internal table can vary. There can be 100 entries with 3,4,5 product types or even more different product types can be appended. Depending upon the type of product, I need to increment a counter variable(type i) and show the count on the report. If the number of product types is fixed, i can use a fixed no of counter variables. But in this case how many counter variables can i use? How can i do this? Pls suggest.
05-17-2016 6:12 PM
05-17-2016 6:12 PM
05-17-2016 6:45 PM
Hi Ricky,
You can use SELECT COUNT statement to count each relevant field according to your criteria.
You can read more here: ABAP Keyword Documentation
For example, to determine the number of airlines flying to New York, you can do the following:
DATA count TYPE i.
SELECT COUNT( DISTINCT carrid )
FROM spfli
INTO count
WHERE cityto = 'NEW YORK'.
Regards,
Felipe
05-17-2016 7:09 PM
Felipe,
This is something to count from internal table entries.
So select Count** will not work here.
05-17-2016 7:16 PM
Ricky,
In this case, you could create a copy of the original internal table and then delete all entries that do not match your criteria. Then, you just count the number of lines in it.
* copy contents from one itab to another of same type
copy_itab[] = original_itab[].
DELETE copy_itab WHERE NOT product_type = 'type1'.
* count lines of itab copy
quantity = LINES( copy_itab ).
Regards,
Felipe
05-17-2016 9:08 PM
Hello Ricky,
Declare another internal table as like original internal table,move the original internal table data into declared internal table, and then sort internal table based on product type and delete adjacent duplicates based on product type.
* copy contents from one itab to another of same type
itab[] = original_itab[].
SORT itab by product_type.
DELETE ADJACENT DUPLICATES FROM itab COMPARING product_type.
DESCRIBE TABLE itab LINES lv_count.
Thanks,Sateesh
05-17-2016 9:42 PM
Oh!
I want to count the number of product types. I don't want to eliminate the duplicates.
05-17-2016 9:46 PM
Ricky,
If you perform the deletions, as per my previous reply or via Sateesh's method, it will achieve the same.
You will have a copy of the original table with only the records that match your criteria, therefore, by counting its lines, you should get the count of each product type.
Regards,
Felipe
05-17-2016 11:17 PM
Hi Felipe,
I am sorry for the confusion.
I need to display the no of times each product type is found and display its count.
I want to increase the counter for every product type even if it repeated.
My concern is how many variables do i need (to count) for every different product type?
So i need to use the internal table with collect statement a suggested by Sandra.
05-18-2016 6:54 AM
You'll get explanations and examples in the ABAP documentation for COLLECT statement.
In short :
Structure and Internal table with 2 components: product type and counter of type I for instance. By default, the key will be product type.
For every line of the main internal table, you fill the structure with the product type and counter equal to value 1, and you use COLLECT to fill the internal table. After the loop, you'll have one line per product type, and the counter will be the number of lines per product type.
05-18-2016 5:20 PM
05-18-2016 2:35 AM
Try this one.
Sort the table by product type.
make use of index checking the current/previous product type
Data:
l_ctr TYPE i, "Counter for # of product type
l_index TYPE sy-tabix.
l_index = 1.
l_ctr = 1.
Sort table BY product type.
LOOP at i_table INTO current.
ADD 1 to l_index.
Read table i_table INTO next INDEX l_index.
"Check if next entry equal to previous record product type
IF current-product_type NE next-product_type.
ADD 1 to l_ctr.
ENDIF.
ENDLOOP.