SQL to find the number of distinct values in a column

前端 未结 11 899
隐瞒了意图╮
隐瞒了意图╮ 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 00:59

    Be aware that Count() ignores null values, so if you need to allow for null as its own distinct value you can do something tricky like:

    select count(distinct my_col)
           + count(distinct Case when my_col is null then 1 else null end)
    from my_table
    /
    
    0 讨论(0)
  • 2020-11-28 01:02

    You can use the DISTINCT keyword within the COUNT aggregate function:

    SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name
    

    This will count only the distinct values for that column.

    0 讨论(0)
  • 2020-11-28 01:04

    **

    Using following SQL we can get the distinct column value count in Oracle 11g.

    **

    Select count(distinct(Column_Name)) from TableName
    
    0 讨论(0)
  • 2020-11-28 01:11

    Count(distinct({fieldname})) is redundant

    Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]

    0 讨论(0)
  • 2020-11-28 01:13
    select count(*) from 
    (
    SELECT distinct column1,column2,column3,column4 FROM abcd
    ) T
    

    This will give count of distinct group of columns.

    0 讨论(0)
  • 2020-11-28 01:15
    select Count(distinct columnName) as columnNameCount from tableName 
    
    0 讨论(0)
提交回复
热议问题