Application Development and Automation 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: 
Read only

Internal table using collect

Former Member
0 Likes
613

Hi,

I have some problem in internal table using collect.

thatis

it1-1 it-2 it-3 it-4(Count of it-2)

1 Nan 30 2

2 Rav 40 1

3 Ram 20 1

4 Raj 20 1

5 Nan 10 2

this is my internal table.

here it-4 is no.of time repeated counted through loop.

Now i am giving collect means no.of time repeated will show all '1' only not no.of times repeated. here i want to count before display and before collect.1 ,2 and 3 fields are collected from database field 4 generated dynamically.

or else i want to copy same fields to another it means field 4 last line count only showing otherslines are displaying 0's

What i can do?.

Thanks

Regards,

Nandha

4 REPLIES 4
Read only

Former Member
0 Likes
550

Nandha,

Keep in the mind that it1 it2 should be of non-numneric fields.

Is that it3 and it4 are numeric ???

so Using COLLECT will add values in it3 and it4 making single row for Nan eg : .

Rav 40 1

Ram 20 1

Raj 20 1

Nan <b>40 4</b>

Thanks

Kam

Read only

0 Likes
550

Hi,

This is ok but i don't want to sum 4th column.4th column value is the count of third column

i.e nan tow times repeated means it'll show 2

that 2 i need without changes

Thanks

Nandha

Read only

0 Likes
550

Hi Nandha,

Can you please give somemore information with a clear example? Please correct me if I am wrong.

You have an internal table ITAB with 4 fields, let us say A, B, C and D. You gave us the values of the internal table as follows


A   B  C
1  Nan 30
2  Rav 40
3  Ram 20
4  Raj 20
5  Nan 10

Now you want to know how many times a value in column B is repeated and have that to be in your 4th column D. I don't think you can achieve that using COLLECT. You have to do it in a loop and by sorting your ITAB. Try this.


SORT itab BY b.
LOOP AT itab.
  IF v_previous_b_value = itab-b.
*-- the value in column B hasn't changed, increment the count
    b_count = b_count + 1.
  ELSE.
*-- it is a new B value, modify the internal table
*   with previous B value count
    IF sy-tabix = 1.
*-- this is the first record, do not do the update
      b_count = 1.
    ELSE.
      itab-d = b_count.
      MODIFY itab TRANSPORTING b_count WHERE b = v_previous_b_value.
      b_count = 1.
    ENDIF.
  ENDIF.
  v_previous_b_value = itab-b.
ENDLOOP.
*-- for the last record
itab-d = b_count.
MODIFY itab TRANSPORTING b_count WHERE b = v_previous_b_value.

I am just typing this out directly without testing, but I think you will get the idea.

Srinivas

Read only

Former Member
0 Likes
550

If the first field in numeric then use of collect would result in

2 Rav 40 1

3 Ram 20 1

4 Raj 20 1

6 Nan 40 4

If it is non-numeric,

then, this will result

1 Nan 30 2

2 Rav 40 1

3 Ram 20 1

4 Raj 20 1

5 Nan 10 2

Regards,

Ravi