count distinct(…) doesn't consider null to be a distinct value:
create table tq84_count_distinct_test ( a number );
begin
insert into tq84_count_distinct_test values ( 1 );
insert into tq84_count_distinct_test values ( 1 );
insert into tq84_count_distinct_test values ( 1 );
insert into tq84_count_distinct_test values ( 2 );
insert into tq84_count_distinct_test values ( 3 );
insert into tq84_count_distinct_test values ( 3 );
insert into tq84_count_distinct_test values ( 3 );
insert into tq84_count_distinct_test values ( 3 );
commit;
end;
/
The following statement returns 3 (which corresponds to the number distinct values in tq84_count_distinct_test):
select
count(distinct a) cnt_dist
from
tq84_count_distinct_test;
A null value is inserted - but the select statement will still return 3:
insert into tq84_count_distinct_test values ( null );
commit;
Using sys_op_map_nonnull gives the alternative (some would say: more accurate) value 4:
select
count(distinct sys_op_map_nonnull(a)) cnt_dist
from
tq84_count_distinct_test;
COUNT(DISTINCT col-1, col_2) raises ORA-00909
Unlike some SQL implementations found in other databases (for example MySQL), Oracle does not allow to use a (comma-separated) list of expressions in the count(distinct …) clause. The following statement raises ORA-00909: invalid number of arguments:
select count(distinct owner, object_type) from all_objects
This shortcoming is typically circumvented with a subquery:
select count(*) from (select distinct owner, object_type from all_objects);