SQL加上内部计数的2列数据

时间:2017-02-25 02:19:30

标签: sql count distinct

我尝试得到结果3,5没有计数重复,NUll值和空数据。

    select count(distinct no1), count(distinct no1) + count(distinct no2) from abc where no1 
    is not null

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

select count(distinct no1), count(distinct no1) + (select  count(distinct no2) from abc where no2 is not null and no2<>'') from abc where no1 is not null

试试这个

答案 1 :(得分:1)

如果你不想要null和空,请试试这个:

select
   count(distinct case when no1 = '' then null else no1 end),
   count(distinct case when no1 = '' then null else no1 end)
   + count(distinct case when no2 = '' then null else no2 end)
from `tbl`

请在此处查看Demo

解释 countsumavg等...像这些聚合函数一样不会{{1作为计算对象。所以只需将空值改为null,这里使用null来执行此操作,然后case when将获得您想要的内容。