HIVE場景題之交差並集

1.4.1使用hive求出兩個數據集的差集
數據
t1表: id name
1zs
2ls

t2表: id name
1 zs
3 ww

結果如下:
id name
2 ls
3 ww

create table diff_t1( id string,
name string
)
row format delimited fields terminated by ’ ’
;
load data local inpath ‘/hivedata/diff_t1.txt’ overwrite into table diff_t1;

create table diff_t2( id string,
name string
)
row format delimited fields terminated by ’ ’
;
load data local inpath ‘/hivedata/diff_t2.txt’ overwrite into table diff_t2;

select t1.id as id, t1.name as name from diff_t1 t1
left join diff_t2 t2 on t1.id=t2.id
where t2.id is null union
select
t2.id as id, t2.name as name from diff_t1 t1
right join diff_t2 t2 on t1.id=t2.id
where t1.id is null
;

1.4.2兩個表A 和B ,均有key 和value 兩個字段,寫一個SQL語句,將B表中的value值置成A表中相同key值對應的value值

在這裏插入圖片描述

create table t1( key string, value string
)
row format delimited fields terminated by " "
;
create table t2( key string, value string
)
row format delimited fields terminated by " "
;
load data local inpath ‘/data/t1’ into table t1; load data local inpath ‘/data/t2’ into table t2;

select a.key,
if(a.value2 is null, a.value1,a.value2) value from
(select t2.key,
t2.value value1, t1.value value2 from t2
left join t1
on t1.key = t2.key
) a
;

1.4.3有用戶表user(uid,name)以及黑名單表Banuser(uid)
1、用left join方式寫sql查出所有不在黑名單的用戶信息
2、用not exists方式寫sql查出所有不在黑名單的用戶信息

select u.id, u.name from u
left join banuser b on u.id=b.id
where b.id is null
;

2、select u.id, u.name from u
where not exists (select 1 from banuser b where u.id=b.id)
;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章