最近查詢數據的時候遇到了一個問題,百思不得其解,具體現象如下:
select count(1) from idm.idm_table1_s_d where dt = '2020-03-18'
結果爲:2053683514
select count(1) from idm.idm_table1_s_d where dt = '2020-03-18' and col_1 in ('1978004','1978003')
結果爲:2053683514
select count(1) from idm.idm_table1_s_d where dt = '2020-03-18' and col_1 not in ('1978004','1978003')
結果爲:192030210
你會發現in 的結果加上 not in 的結果 並不等於總數。後經過多方查詢及請教,才找到問題的根源:
我的數據源表的col_1是有空值的,而hive的where條件中使用的not in 或者in時,隱藏了 is not null 的條件。
select count(1) from idm.idm_table1_s_d where dt = '2020-03-18' and col_1 is null
結果爲:1835016104
這樣你會發現總數=not in 的數量+in的數量+is null的數量。
即where col_1 in ('1978004','1978003') 其實等效於 where col_1 in ('1978004','1978003') and col_1 is not null