配置单元中的sql自连接查询

kyks70gy  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(366)

我需要一个配置单元查询来获取销售产品的层次结构。考虑到下面的记录,最终客户是1和6,因为它们的soldto列值是空的。

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
svgewumm

svgewumm1#

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;

相关问题