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?
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?
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.
(11,1,1)
and (1, 1, 11)
), so you at minimum need to know something about your domain, and will almost always require a separator be added. Plus the concatenation will be slow on a table of any real size. @user2329435 - what do you mean "doesn't work"? That's the proper way to structure that type of query. –
Forester select count(*) from (select distinct col1, col2, col3 FROM table) as correlationname
works. –
Odie select count(*), count(distinct(*)), count(distinct col1 || col2 || col3) from table
). –
Timbering select count(distinct col1 || '^' || col2 || '^' || col3) from table
to avoid problems during concatenation like between 1 || 11 which would be the same as 11 || 1.
© 2022 - 2024 — McMap. All rights reserved.