sql count中加条件

一般的,我们会在where, 或者 having中加条件,count中只是某个字段

今天看到另外一种写法,不知道性能怎么样

select 

count(  case    when xxx>10 and yyy<99 then bbb else null end)  cm1,

count(  case    when xxx>20 and yyy<1 then ccc  else null end)  cm2

from xxxx

基本原理是 使用了数据库count(null)时返回的是0 count(非null)返回是1

@Flink SQL

使用sum(case when xxx then 1 else 0) 替换count

在group 中使用多个聚合函数:

select count(xxxx) + sum(xxxx)
group by xxx
posted @ 2019-02-25 13:42  funny_coding  阅读(21893)  评论(0编辑  收藏  举报
build beautiful things, share happiness