1. 程式人生 > >hive的union all語句的順序問題

hive的union all語句的順序問題

有這麼一個查詢:

select -count(*) from
(select distinct ${hiveconf:colname} from ${hiveconf:tablename}
where ${hiveconf:latest_status}) t
union all
select count(*) from ${hiveconf:tablename}
where ${hiveconf:latest_status} and (${hiveconf:colname}='NULL' or ${hiveconf:colname} is null);

查詢結果卻是下面這樣:

id _c0
0 26316870
1 -1187700

很明顯,count(*)的結果必然非負,第一個count(*)的前面我人為加了負號以便區別。

可以看出union all出來的結果的順序與程式碼中的順序不一致,因此在使用union all語句的時候,需要格外注意,尤其是對結果的順序敏感的話,應當通過適當的方法來加以標識,比如本例中對其中一個加了負號