SQL to find the number of distinct values in a column

前端 未结 11 884
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 00:19

I can select all the distinct values in a column in the following ways:

  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_
相关标签:
11条回答
  • 2020-11-28 01:17

    After MS SQL Server 2012, you can use window function too.

       SELECT column_name, 
       COUNT(column_name) OVER (Partition by column_name) 
       FROM table_name group by column_name ; 
    
    0 讨论(0)
  • 2020-11-28 01:21

    This will give you BOTH the distinct column values and the count of each value. I usually find that I want to know both pieces of information.

    SELECT [columnName], count([columnName]) AS CountOf
    FROM [tableName]
    GROUP BY [columnName]
    
    0 讨论(0)
  • 2020-11-28 01:22

    An sql sum of column_name's unique values and sorted by the frequency:

    SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY 2 DESC;
    
    0 讨论(0)
  • 2020-11-28 01:22
    SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;
    

    you've got to count that distinct col, then give it an alias.

    0 讨论(0)
  • 2020-11-28 01:23
    select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true
    

    You can use this query, to count different/distinct data. Thanks

    0 讨论(0)
提交回复
热议问题