Count distinct value pairs in multiple columns in SQL

user1866731 picture user1866731 · Sep 10, 2013 · Viewed 103.8k times · Source

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

Answer

gvee picture gvee · Sep 10, 2013

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