DROP table if exists tmp_test_insert_none_yc_table;
create table tmp_test_insert_none_yc_table(
user_id string comment '记录首次时间'
,start_date string comment '记录首次时间'
,end_date string comment '记录最后一次登录时间'
) comment 'user_id粒度的汇总表'
PARTITIONED BY
(
dt STRING COMMENT '分区日期'
)
lifecycle 2;
insert overwrite table tttttttt partition(dt = '20200915') VALUES
("a", 1, 1),
("b", 1, 1),
("b", 1, 1),
("c", 1, 1),
(null, 1, 1);
SELECT
DISTINCT user_id --- 3,算上了null
FROM
tttttttt;
SELECT
count(1) ----- 5
, count(DISTINCT user_id) --- 2,不算null
, count(CASE WHEN user_id = "b" THEN 1 end) --- 2
, count(distinct CASE WHEN user_id = "b" THEN 1 end) ---- 1
, count(distinct CASE WHEN user_id = "b" THEN user_id end) ---- 1,和上条一样,有没有id都无所谓
, count(CASE WHEN user_id = "a" THEN 1 end) ---- 1
, count(CASE WHEN user_id is not null THEN 1 end) ---- 4
, count(CASE WHEN user_id != null THEN 1 end) ---- 0
FROM
tttttttt;
【SQL】论count 和 distinct
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...