SQL to find the number of distinct values in a column

前端 未结 11 1027
隐瞒了意图╮
隐瞒了意图╮ 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
    /
    

提交回复
热议问题