Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count distinct value pairs in multiple columns in SQL

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 
like image 471
user1866731 Avatar asked Sep 10 '13 09:09

user1866731


People also ask

How do I count distinct values over multiple columns in SQL?

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);

Can I use distinct with multiple columns?

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.

How do you count unique pairs?

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.

How do I find unique two column combinations in SQL?

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.


1 Answers

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 
like image 120
gvee Avatar answered Sep 19 '22 06:09

gvee