背景:hive中的null在底层是以“\N”来存储;
问题:当查询条件为,限制int类型字段!= 1时,“!=”会将null直接过滤掉,导致数据丢失;
举例:
--industry_id 为int型
--结果1:6863239
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and (a.industry_id is null or a.industry_id <> 1)
UNION all
--结果2:6863239
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and coalesce(a.industry_id,'') <> '1'
union all
--结果3:6863239
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and coalesce(a.industry_id,-99) <> 1
union all
--结果4:6863221
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and a.industry_id <> '1'
union all
--结果5:6863221
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and cast(a.industry_id as string) <> '1'
union all
--结果6:6863221
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and a.industry_id <> 1
union all
--结果7:18
SELECT count(*) from bi_temp.temp_drp_tsp_bill_order_city_base_dwm_01 a
where bi_category_id in (100000) and a.industry_id is null
;
解决办法:
方法一:将字段null值给int型默认值,再进行判断(见结果3);
方法二:将判断条件的统一为string(见结果2);
方法三:综合判断(见结果1)。

1320

被折叠的 条评论
为什么被折叠?



