HIVE SQL 根据主键去重并实现其余字段分组聚合

相同个人id下所有字段按时间顺序补位,取首个不为空值
在这里插入图片描述

--数据建表
drop table if exists db.tb_name;
create table if not exists db.tb_name
( id string
	,name string
	,tele string
	,email string
	,`date` string
)
;
insert overwrite table db.tb_name
values 
("32001","张三","23456789",null,"2023-07-18")
,("32001",null,null,"23456789@163.com","2023-07-19")
,("32002","李四",null,"23456876@qq.com","2023-07-18")
,("32003","王二",null,null,"2023-07-18")
,("32003",null,"9876789",null,"2023-06-18")
,("32003",null,null,"9876789@gmail.com","2023-07-18")
,("32004","刘五","987456798",null,"2023-07-18")
,("32004","刘七","1987456798",null,"2023-07-20")
;
--distribute by 分区排序:类似MR中partition,进行分区,结合sort by使用

drop table if exists db.tb_name_new;
create table if not exists db.tb_name_new as
select id 
	,collect_list(`name`)[0] as `name`
	,collect_list(`tele`)[0] as `tele`
	,collect_list(`email`)[0] as `email`
	,collect_list(`date`)[0] as `date`
from 
(
select id
	,name
	,tele
	,email
	,`date`
from 
db.tb_name
distribute by id 
sort by id,`date` desc
) t 
group by id
;

*注意:此处是取的首个不为空(即不为null)的字段,所以在实际使用过程中应提前将空字符串转为null值。

--剔除字符串中的不可见字符,若该字段中均为不可见字符或该字段为空字符串,则转为空
case when length(regexp_replace(col_name,'[\\x00-\\x08\\x0B-\\x0C\\x0E-\\x1F]+|\\s+',''))>0 
	then regexp_replace(col_name,'[\\x00-\\x08\\x0B-\\x0C\\x0E-\\x1F]+|\\s+','')
	else null end as new_col_name
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值