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

Count the values

Former Member
0 Likes
415

Hai Friends,

1 A 10

1 A 20

1 A 30

1 B 30

1 B 20

1 C 20

I want the count of 1st column when there is a change in second column. That is,

1 A 10

1 A 20

1 A 30 count is 3

1 B 30

1 B 20 count is 2

1 C 20 count is 1

Thanks.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
390

Hello

The itab must be sorted accordingly:


DATA: ld_sum             TYPE i,
DATA: ls_record         LIKE LINE OF lt_itab,
DATA: ls_record_old  LIKE LINE OF lt_itab.

  SORT lt_itab BY column2.
  
  ld_sum = 0.
  READ TABLE lt_itab INTO ls_record_old INDEX 1.
  LOOP AT lt_itab INTO ls_record.

    IF ( ls_record-column2 = ls_record_old-column2 ).
     ADD ls_record-column1 TO ld_sum.
    ELSE.
      " NOTE: Here you have store the value of LD_SUM somewhere

      ld_sum = ls_record-column1.
    ENDIF.

    ls_record_old = ls_record.
  ENDLOOP.

Regards

Uwe

3 REPLIES 3
Read only

uwe_schieferstein
Active Contributor
0 Likes
391

Hello

The itab must be sorted accordingly:


DATA: ld_sum             TYPE i,
DATA: ls_record         LIKE LINE OF lt_itab,
DATA: ls_record_old  LIKE LINE OF lt_itab.

  SORT lt_itab BY column2.
  
  ld_sum = 0.
  READ TABLE lt_itab INTO ls_record_old INDEX 1.
  LOOP AT lt_itab INTO ls_record.

    IF ( ls_record-column2 = ls_record_old-column2 ).
     ADD ls_record-column1 TO ld_sum.
    ELSE.
      " NOTE: Here you have store the value of LD_SUM somewhere

      ld_sum = ls_record-column1.
    ENDIF.

    ls_record_old = ls_record.
  ENDLOOP.

Regards

Uwe

Read only

Former Member
0 Likes
390

Hi,

You can keep the logic simple by this approach.

Step 1: Sort the internal table by the 2nd Column.

Step 2: By processing the individual lines of the internal table you can track the changes.

The code extract is given below:

Data : Count type i value 0.

Loop at itab.

at new column2.

count = 0.

endat.

count = count + 1.

  • your own logic here.

endloop.

Hope this helps.

Thanks,

Samantak.

Read only

Former Member
0 Likes
390

Hi,

Please check this


DATA: BEGIN OF itab OCCURS 0,
        f1 TYPE char10,
        f2 TYPE char10,
        f3 TYPE char10,
      END OF itab.

DATA: count TYPE i.

itab-f1 = '1'.
itab-f2 = 'A'.
itab-f3 = '10'.
APPEND itab.

itab-f1 = '1'.
itab-f2 = 'A'.
itab-f3 = '20'.
APPEND itab.

itab-f1 = '1'.
itab-f2 = 'A'.
itab-f3 = '30'.
APPEND itab.

itab-f1 = '1'.
itab-f2 = 'B'.
itab-f3 = '30'.
APPEND itab.

itab-f1 = '1'.
itab-f2 = 'B'.
itab-f3 = '20'.
APPEND itab.

itab-f1 = '1'.
itab-f2 = 'C'.
itab-f3 = '20'.
APPEND itab.

count = 0.
LOOP AT itab.
  WRITE:/ itab-f1, itab-f2, itab-f3.
  count = count + 1.
  AT END OF f2.
    WRITE:/'Count is ', count.
    CLEAR count.
  ENDAT.

ENDLOOP.