hive 列表去重 HIVE的多表查詢

2021-10-14 08:33:40 字數 2804 閱讀 6015

先來看看union all語句的使用

union all語句將倆個表中相同的字段拼接在一起進行表的查詢,注意的是union all不去重,資料會重複,我們來看看網路上的一些案例~

--準備表create external table if not exists temp_uniontest_ta(a1 string,a2 string)partitioned by (dt string)row format delimited fields terminated by ''stored as textfile;alter table temp_uniontest_ta add if not exists partition (dt = '2014-10-13') location '/temp/uniontest/ta/';a1 a2 2014-10-13b1 b2 2014-10-13c1 c2 2014-10-13 create external table if not exists temp_uniontest_tb(a1 string,a2 string)partitioned by (dt string)row format delimited fields terminated by ''stored as textfile;alter table temp_uniontest_tb add if not exists partition (dt = '2014-10-13') location '/temp/uniontest/tb/';d1 d2 2014-10-13e1 e2 2014-10-13
-- 進行union all查詢select * from (select a1,a2 from temp_uniontest_ta where dt = '2014-10-13'union allselect a1,a2 from temp_uniontest_tb where dt = '2014-10-13') tmp;a1 a2b1 b2c1 c2d1 d2e1 e2
上面是我們的union all連線,接下來我們看看hive的join連線

在這裡要注意的是hive只支援等值連線(equality joins)、外連線(outer joins)和(left/right joins)。hive 不支援所有非等值的連線,因為非等值連線非常難轉化到 map/reduce 任務。另外,hive 支援多於 2 個表的連線。join關聯查詢例項如下:

innser join(join)查詢

left outer join(left join)查詢

full outer join(full join)查詢

left semi join查詢

hive中不支援exist/in子查詢,可以用left semi join來實現同樣的效果:

注意: left semi join的 select子句中,不能有右表的字段

上述是我們常用的join查詢,接下來我們看看我們最常用的with語法:

為啥要說with呢,首先我們用的很多,賊多,hive可以通過with查詢來提高查詢效能,因為先通過with語法將資料查詢到記憶體,然後後面其他查詢可以直接使用~案例如下:

with q1 as ( select key from q2 where key = '5'),q2 as ( select key from src where key = '5')select * from (select key from q1) a;--上述**中q1是一張表,q2也是一張表,這樣我們就可以講複雜的sql語句簡化一部分,個人覺得特別好用
--插入操作例項create table s1 like src;with q1 as ( select key, value from src where key = '5')from q1insert overwrite table s1select *;
基礎的語法就如上所示啦,在實際應用中我們一般會將with,union all,if,統計函式,group by 等結合起來一起操作,一條sql56十行都是正常的,所以建議大家打好基礎方便後期的使用~~~~

最終在奉勸圖靈的廣大學子,別做伸手黨,想要成長的快點,就必須自己去操作去練習~學習是沒有捷徑的~

hive 列表去重 Hive 資料去重

實現資料去重有兩種方式 distinct 和 group by 1.distinct消除重複行 distinct支援單列 多列的去重方式。單列去重的方式簡明易懂,即相同值只保留1個。多列的去重則是根據指定的去重的列資訊來進行,即只有所有指定的列資訊都相同,才會被認為是重複的資訊。1 作用於單列 se...

Hive資料去重

hive資料去重 insert overwrite table ta customers select t.ta id,t.ta date from select ta id,ta date row number over distribute by ta id sort by ta date de...

hive 去重 字串 hive 函式

substr string a,int start,int len substring string a,intstart,int len 用法一樣,三個引數 返回值 string 說明 返回字串a從start位置開始,長度為len的字串,下標預設為1.若沒有長度預設到結尾。round round ...