Re: SQL: count distinct multi-column
Posted in 1997
> I also tried the following to no avail:
> SELECT COUNT( DISTINCT k1||k2||k3) FROM tbl;>
> Note that the query without the count works OK. Also there are too many
> rows to use "GROUP BY k1,k2,k3".
Don't do this, you are creating a single column made up of the
3 columns concateneted together, hence the "too many rows" error
as you are only creating one.
Some people try to add a "where" based on the amalgamation, but this
is dangerous as it creates a sequential scan on the table, even
if the 3 columns are indexed, the index will not be used for the
amalgamation.
--
------------------------------------------
The views expressed here are mine and not
those of my employer, Britannic Assurance
------------------------------------------