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: 

Mutiple Counter variables

ricky_shaw
Contributor
0 Kudos

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.

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

You may count using an internal table and COLLECT statement.

11 REPLIES 11

Sandra_Rossi
Active Contributor
0 Kudos

You may count using an internal table and COLLECT statement.

former_member198833
Active Participant
0 Kudos

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

0 Kudos

Felipe,

This is something to count from internal table entries.

So select Count** will not work here.

0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

Oh!

I want to count the number of product types. I don't want to eliminate the duplicates.

0 Kudos

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

ricky_shaw
Contributor
0 Kudos

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.

0 Kudos

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.

0 Kudos

That's what i did Sandra 🙂

Thanks

Former Member
0 Kudos

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.