[英]Self join queries in hive
我需要一个配置单元查询来获取销售产品的层次结构。 考虑以下记录,最终客户为1和6,因为其SoldTo列值为NULL。
CustomerID SoldTo
--------------------
1 NULL
2 1
3 2
4 3
5 4
6 NULL
7 1
8 6
我的输出应如下所示:
c1 c2 c3 c4 c5
-------------------
5 4 3 2 1 (c1 (5) - first customer who bought product and c5(1) -last customer)
8 6 (c1 (8) - first customer , c2 (6)- Last customer)
7 1
Hive没有对递归CTE或分层数据结构的真正支持。 您可以使用多个联接来执行此操作-但层次结构的深度是固定的。
select t1.CustomerId as c1, t2.CustomerId as c2, t3.CustomerId as c3,
t4.CustomerId as c4, t5.CustomerId
from t t1 left join
t t2
on t2.SoldTo = t1.CustomerId left join
t t3
on t3.SoldTo = t2.CustomerId left join
t t4
on t4.SoldTo = t3.CustomerId left join
t t5
on t5.SoldTo = t4.CustomerId
where t1.CustomerId is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.