首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL +2列数据内部计数

SQL +2列数据内部计数
EN

Stack Overflow用户
提问于 2017-02-25 02:19:29
回答 2查看 55关注 0票数 0

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

代码语言:javascript
复制
    select count(distinct no1), count(distinct no1) + count(distinct no2) from abc where no1 
    is not null

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-25 02:53:03

如果不希望空和空,请尝试如下:

代码语言:javascript
复制
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等。像这些聚合函数一样,它不会将null作为其计算对象。因此,只需将空值转换为null,在这里使用case when来完成它,然后count distinct就会得到您想要的。

票数 1
EN

Stack Overflow用户

发布于 2017-02-25 02:25:43

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42451240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档