hive-sql语句对in和not in的替换

Hive SQL不支持子查询中的多个not in或in操作。解决方法是使用left join,将not in替换为left join后字段为空,in替换为left join后字段不为空,或者利用left semi join。同时,Hive的日期函数不接受'%'格式,仅支持直接日期字符串。文章还分享了一个复杂的查询案例的替换方法。
摘要由CSDN通过智能技术生成

对于hive-sql里的子查询不支持not in或in ,目前测试,应该是一个hive语句里只能支持一个not in 或in语句,多了不支持,对not in的替换用 left join id(关联字段)is null ,in的替换用left join id is not null替换,或者用left semi join(更优化)

not in 的替换示例

--原sql
select id from a
where id not in (select id from b);

--hive-sql替换示例
select id from a
left  join b  on b.id=a.id
where b.id is null;

in 替换示例

--原sql
select id from a
where id in (select id from b);

--hive-sql替换示例
select id from a
left  join b  on b.id=a.id
where b.id is not null;
--或
select id from a
left  semi join b  on a.id=b.id

hive杂记
hivesql里的日期函数不支持’%Y-%m-%d’这样的表达,虽然不会报错,但是不会起作用
支持’2019-06-16’这样的表达
顺便记下我自己做的一个比较复杂的案例
原sql

SELECT
  count(DISTINCT t.passport_user_id) 新增用户数,
  count( t.passport_user_id) 新增用户交易笔数,
  sum(t.trade_amount) 新增用户交易金额
FROM t_trade t
INNER JOIN t_user u ON u.passport_user_
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值