case count(distinct是什么意思

lilinew 2010-10-22 06:46:01
select
User.ID as UserID,
case count(distinct
case
when Amount>0 or Fee>0 then userid
else null
end
)
when 0 then 0
else 1
end ,
case count( distinct
case IsEnabled
when 1 then userid
else null
end)
when 0 then 0
else 1
end ,
accontID from a.......

这里
case count(distinct
case
when Amount>0 or Fee>0 then userid
else null
end
)
when 0 then 0
else 1
end ,
case count( distinct
case IsEnabled
when 1 then userid
else null
end)
when 0 then 0
else 1
end 是什么意思 ??
...全文
564 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilinew 2010-10-24
  • 打赏
  • 举报
回复
select count(distinct null)
;
+----------------------+
| count(distinct null) |
+----------------------+
| 0 |
+----------------------+
1 row in set

-------------可行的
abuying 2010-10-22
  • 打赏
  • 举报
回复
1:统计满足Amount>0 or Fee>0 条件不重复userid的个数,
如果为0,没有此条件userid,则为0,如果有,则为1

2,采用case嵌套
统计满足IsEnabled=1条件不重复userid的个数
如果为0,没有此条件userid,则为0,如果有,则为1
aisail 2010-10-22
  • 打赏
  • 举报
回复
去除重复值,重复值只计算一次
lilinew 2010-10-22
  • 打赏
  • 举报
回复
选择一个
case count(distinct case when Amount>0 or Fee>0 then userid else null end )
when 0 then 0
else 1
end ,

这样明白了点

应该是这个意思

case when Amount>0 or Fee>0 那个count(distinct userId)的数据 ,否则 count(distinct null)
如果count(distinct结果为0--》 0
count(distinct结果不为0 ---》 1
SQLCenter 2010-10-22
  • 打赏
  • 举报
回复
简化

sign(count(case when Amount>0 or Fee>0 then userid end)),
sign(count(case IsEnabled when 1 then userid end))
SQLCenter 2010-10-22
  • 打赏
  • 举报
回复
这个逻辑又为何要distinct呢?
SQLCenter 2010-10-22
  • 打赏
  • 举报
回复
V1:Amount>0 or Fee>0 的不重复 userid 数
V1=0 ? 0 : 1

V2: IsEnabled=1 的不重复 userid 数
V2=0 ? 0 : 1

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧