Count distinct multiple columns in redshift

You can use

select col1,col2,count(*) from schemaname.tablename
where -- your filter
group by col1,col2

A little late to the party but anyway: you can also try to concatenate columns using || operator. It might be inefficient so I wouldn't use it in prod code, but for ad-hoc analysis should be fine.

select count(distinct col1 || '_' || col2)
from schemaname.tablename
where some filters

Note separator choice might matter, i.e. both 'foo' || '_' || 'bar_baz' and 'foo_bar' || '_' || 'baz' yield 'foo_bar_baz' and are thus equal. In some cases this might be concern, in some it's so insignificant you can skip separator completely.


you can use sub-query and count

select count(*) from (
  select distinct col1, col2 
 from schemaname.tablename
  where some filter
) as t