What query will count the number of rows, but distinct by three parameters?
Example:
Id Name Address ============================== 1 MyName MyAddress 2 MySecondName Address2
Something like:
select count(distinct id,name,address) from mytable
but when we want to count distinct column combinations, we must either clumsily concatenate values (and be very careful to choose the right separator): select count(distinct col1 || '-' || col2) from mytable; or use a subquery: select count(*) from (select distinct col1, col2 from mytable);
Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns.
Efficient approach: First find out the number of unique elements in an array. Let the number of unique elements be x. Then, the number of unique pairs would be x2. This is because each unique element can form a pair with every other unique element including itself.
Select with distinct on all columns of the first query. Select with distinct on multiple columns and order by clause. Count() function and select with distinct on multiple columns.
To get a count of the number of unique combinations of id
, name
and address
:
SELECT Count(*) FROM ( SELECT DISTINCT id , name , address FROM your_table ) As distinctified
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With