How can I count unique pairs of values in SQL?

前端 未结 4 836
别那么骄傲
别那么骄傲 2021-01-01 15:58

With the following sql statement I can get all unique values with their counts for a given column:

select column, count(column) as count 
         from table          


        
相关标签:
4条回答
  • 2021-01-01 16:19
    SELECT first_name, last_name, COUNT(distinct last_name) AS c
    FROM ttable
    GROUP BY first_name, last_name
    HAVING c > 999
    ORDER BY c DESC 
    

    Adding distinct will do it in MYSQL. Thanks

    0 讨论(0)
  • 2021-01-01 16:21

    If you just want the count of how many distinct pairs, you could do this more simply. A GROUP BY clause is not necessary.

    SELECT COUNT(DISTINCT first_name, last_name) AS count_names FROM Table
    
    0 讨论(0)
  • 2021-01-01 16:32

    Use multiple columns in your group by clause.

    select first_name, last_name, count(*) as count from table group by first_name, last_name
    
    0 讨论(0)
  • 2021-01-01 16:44

    You've almost got it correct... You just add an additional GROUP BY column like so:

    SELECT [first_name], [last_name], COUNT(*) AS [Count] 
    FROM [Table]
    GROUP BY [first_name], [last_name]
    ORDER BY [Count] DESC;
    
    0 讨论(0)
提交回复
热议问题