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 field

Former Member
0 Likes
1,061

I have an internal table and is interesting in information in one field. I want to know how many different values there are in one field. How can I do this?

I have an internal table and is interesting in information in one field. I want to know how many different values there are in one field. How can I do this?

9 REPLIES 9
Read only

Former Member
0 Likes
1,039

this will work if field1 is the first field in table....

data ctr type i.

sort itab by field1.

loop at itab.

at new field1.

add 1 to ctr.

...<other code here>

endat.

endloop.

ctr will contain different number of values in the field.

if its not the first field...try something like this...

data ctr type i.

data f1 like itab-field1.

sort itab by field1.

loop at itab.

if f1 ne itab-field1.

add 1 to ctr.

endif.

f1 = itab-field1.

endloop.

rgds,

PJ

Message was edited by: Priyank Jain

Read only

0 Likes
1,039

It is not the first field.

I want to do something like this:

COUNT DISTINCT idnumber IN TABLE itab.

Read only

0 Likes
1,039

Hi,

data: begin of count occurs 0,

field type ...,

munber type i,

end of count.

loop at itab.

move-corresponding itab to count.

count-number = count-number + 1.

collect count.

endloop.

Svetlin

Read only

0 Likes
1,039

Hi,

You can copy it to another itab, sort that itab, do DELETE ADJACENT DUPLICATES FROM itab COMPARING {comp1 comp2 ...} and do a lines( itab )

Eddy

Read only

0 Likes
1,039

hi,

try with my previous soltion

itab1[] = itab[]

sort itab1 by idnumber

delete adjucant duplicates

descripe table

cheers,

sasi

Read only

0 Likes
1,039

Hi,

Pls don't forget to reward points and close the question if you find the answers useful.

Eddy

Read only

0 Likes
1,039

Try this code. It sorts the itab and compares the first one with the next, and the next, until they don't match and then starts comparing the new one, with its next and so on. This way you can get the count of unique values in your itab without defining another itab and it is quicker.


REPORT ztest1 .


DATA: BEGIN OF itab OCCURS 0,
        value  TYPE c,
        id(10) TYPE n.
DATA: END OF itab.

DATA: v_lines TYPE i,
      v_index LIKE sy-tabix,
      v_count TYPE i,
      v_id LIKE itab-id.

START-OF-SELECTION.

  itab-id = 1.
  itab-value = 'A'.
  APPEND itab.
  CLEAR itab.

  itab-id = 2.
  itab-value = 'B'.
  APPEND itab.
  CLEAR itab.

  itab-id = 1.
  itab-value = 'C'.
  APPEND itab.
  CLEAR itab.

  itab-id = 6.
  itab-value = 'D'.
  APPEND itab.
  CLEAR itab.

  itab-id = 3.
  itab-value = 'B'.
  APPEND itab.
  CLEAR itab.

  itab-id = 1.
  itab-value = 'C'.
  APPEND itab.
  CLEAR itab.

  itab-id = 3.
  itab-value = 'B'.
  APPEND itab.
  CLEAR itab.

  itab-id = 4.
  itab-value = 'C'.
  APPEND itab.
  CLEAR itab.

  itab-id = 5.
  itab-value = 'C'.
  APPEND itab.
  CLEAR itab.

  SORT itab BY id.
  DESCRIBE TABLE itab LINES v_lines.

  v_index = 1.
  v_count = 1.
  DO.
    IF v_index = 1.
      READ TABLE itab INDEX v_index.
      v_id = itab-id.
*-- read the next
      v_index = v_index + 1.
    ENDIF.
    IF v_index > v_lines.
      EXIT.
    ENDIF.
    READ TABLE itab INDEX v_index.
    IF itab-id = v_id.
*-- it is same as the previous one, ignore
    ELSE.
*-- it is a different one, add 1 to count
      v_count = v_count + 1.
    ENDIF.
    v_index = v_index + 1.
    v_id = itab-id.
  ENDDO.

  WRITE:/ v_count.

Read only

Former Member
0 Likes
1,039

let me take like that

itab-info = 1

itab-info = 1

itab-info = 2

itab-info = 3

itab-info = 3

you want like

itab-info = 1

itab-info = 2

itab-info = 3

if the above assumption is correct then

define one more itab say itab1

itab1[] = itab.

sort itab1 by info.

delete adjacent duplicates from itab1 comparing info

descripe table itab1 lines n

cheers,

sasi

Read only

andreas_mann3
Active Contributor
0 Likes
1,039

Hi ,

save diff. values of your field in another int. tab

e.g. field is land1 of table itab

REPORT zforum60 .

DATA: BEGIN OF itab OCCURS 0,
        bukrs TYPE t001-bukrs,
        butxt TYPE t001-butxt,
        ort01 TYPE t001-ort01,
        land1 TYPE t001-land1,
      END OF itab.
PARAMETERS fieldn(30) DEFAULT 'LAND1'.
SELECT-OPTIONS s_bukrs FOR itab-bukrs.
DATA: BEGIN OF atab OCCURS 0,
      str TYPE string,
      END OF atab.
FIELD-SYMBOLS <f>.
*
SELECT bukrs butxt ort01 land1 FROM  t001 INTO TABLE itab
       WHERE  bukrs  IN s_bukrs.

CONCATENATE 'ITAB-' fieldn INTO fieldn.
ASSIGN (fieldn) TO <f>.
LOOP AT itab.
  MOVE <f> TO atab-str.
  COLLECT atab.
ENDLOOP.

LOOP AT atab.
  WRITE: / atab-str.
ENDLOOP.
ULINE.
DESCRIBE TABLE atab LINES sy-tfill.
WRITE: / 'cnt:', sy-tfill.

Andreas