2005 Oct 19 12:16 PM
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?
2005 Oct 19 12:21 PM
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
2005 Oct 19 12:23 PM
It is not the first field.
I want to do something like this:
COUNT DISTINCT idnumber IN TABLE itab.
2005 Oct 19 12:32 PM
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
2005 Oct 19 12:32 PM
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
2005 Oct 19 12:45 PM
hi,
try with my previous soltion
itab1[] = itab[]
sort itab1 by idnumber
delete adjucant duplicates
descripe table
cheers,
sasi
2005 Oct 21 9:06 AM
Hi,
Pls don't forget to reward points and close the question if you find the answers useful.
Eddy
2005 Oct 21 9:33 PM
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.
2005 Oct 19 12:22 PM
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
2005 Oct 19 12:36 PM
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
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |