How do I select distinct count over multiple columns?

Tonan picture Tonan · Jan 9, 2018 · Viewed 13.8k times · Source

How to select distinct count over multiple columns?

SELECT COUNT(DISTINCT col1, col2, col3) FROM table; 

Is there a working equivalent of this in DB2?

Answer

data_henrik picture data_henrik · Jan 9, 2018

There are multiple options:

select count(*) from
   (select distinct col1, col2, col3 FROM table) t

The other would be to combine the columns via a CONCAT:

select count(distinct col1 || col2 || col3) from table

The first option is the cleaner (and likely faster) one.