数据库去重汇总 count(distinct xxx) 与distinct ; count(case when )

迷南。 2023-06-28 11:19 29阅读 0赞

总括:
SELECT
T.dept_one as “部门”,
COUNT(DISTINCT (
CASE
WHEN (is_week=’1’) THEN
name
ELSE
NULL
end
)) as “周活”

FROM
(SELECT m.*, s.event_property_code,s.event_property_value FROM default.daily_new_clientrpt_master m LEFT JOIN default.daily_new_clientrpt_slave s on m.event_id= s.event_id ) T where server_time >=toDate(now()) GROUP BY T.dept_one ORDER BY “周活” DESC;

disctinct是对其后所有字段去重,不是对某一个字段 ,clickhouse版本要高19.17

例如:

select a,b,c

select a,b,distinct c 不对 distinct必须是对所有字段排重,必须写在外面。

select distinct a,b,c

第二种 也叫转置 主要理解意思

select a,count(distinct b) ….group by a

按照a,汇总不同的b。

1 1

1 2

1 3

1 2

汇总后:

1 3

count(distinct case when time_measure = ‘>1’ then ISR else null end) 去排重 汇总

sum(case when time_measure = ‘>1’ then 1 else 0 end ) 汇总不排重

点赞
————————————————
版权声明:本文为CSDN博主「waj89757」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tracymm19891990/article/details/8874406

发表评论

表情:
评论列表 (有 0 条评论,29人围观)

还没有评论,来说两句吧...

相关阅读