Skip to Main Content
  • Questions
  • count of distinct on multiple columns does not work

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Rajneesh.

Asked: July 15, 2016 - 10:01 am UTC

Last updated: July 18, 2016 - 4:11 pm UTC

Version: 11.2.0.4.0

Viewed 10K+ times! This question is

You Asked

Hi,

I am trying to count the number of distinct combinations in a table but the query gives error.

For example,

create table t(a varchar2(10), b varchar2(10), c varchar2(10));
insert into t values('a','b','c');
insert into t values('d','e','f');
insert into t values('g','h','i');
insert into t values('j','k','l');

select distinct a,b,c from t;
---4 rows
select count(distinct a,b,c) from t;
error: invalid number of arguments

If I concatenate the columns like below, it works fine, can you please explain this behaviour.

select count(distinct a||b||c) from t;
---4

Regards,
Rajneesh

and Connor said...

That's just the way count-distinct works.

You could also do something like this:

select count(*)
from 
  ( select distinct a,b,c from t )

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Use solution in reply

Erik van Roon, July 18, 2016 - 6:20 am UTC

In fact, the solution in the reply isn't something you can 'also' do (implying the original one was good too) it's what you *should* do.

Suppose you enter an extra record:
insert into t values('ab',null,'c');

Now the working query in he question produces wrong results.
Connor McDonald
July 18, 2016 - 4:11 pm UTC

Great point Erik, thanks for stopping by.