hive-维表关联,维表层级id不确定情况

该博客探讨了如何处理不确定级别的类目ID,以维护到事实表中。通过ODS_classify原始业务库表和预处理的dim_classify类目表,使用LEFT JOIN进行多级类目信息的关联,确保即使只记录了二级类目ID,也能填充完整的一级和三级类目数据。这种方法提高了数据完整性和查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

问题描述:事实表存储的类目id不确定是几级id;一共有三级类目,需要把类目信息维护进事实表,比如记录的是二级类目id,需要把一级类目数据维护进事实表。

一开始没有想到怎么搞,还是想了一会儿想了这么个处理方法:

ods_classify原始业务库表,id就是各级的类目id,pid就是上一层级的类目id,dim_classify是事先处理好的一二三级类目表

select 	t1.id
		,t4.third_id
        ,t4.third_title
        ,case 	when t4.sencond_id is not null then t4.sencond_id
         else 	t3.sencond_id end as sencond_id
        ,case 	when t4.sencond_title is not null then t4.sencond_title
         else 	t3.sencond_title end as sencond_title
        ,case 	when t4.first_id is not null then t4.first_id
        		when t3.first_id is not null then t3.first_id
         else 	t2.first_id end as first_id
        ,case 	when t4.first_title is not null then t4.first_title
        		when t3.first_title is not null then t3.first_title
         else 	t2.first_title end as first_title
from 	(
			select 	id
			from 	ods_classify
		) t1 left
join 	(
            SELECT  first_id
                    ,first_title
            FROM    dim_classify
            group by first_id
                     ,first_title
        ) t2
on 		t1.id = t2.first_id left
join 	(
			SELECT  sencond_id
                    ,sencond_title
                    ,first_id
                    ,first_title
            FROM    dim_classify
            group by sencond_id
                     ,sencond_title
                     ,first_id
                     ,first_title
		) t3
on 		t1.id = t3.sencond_id left
join 	(
			SELECT  third_id
                    ,third_title
                    ,sencond_id
                    ,sencond_title
                    ,first_id
                    ,first_title
            FROM    dim_classify
            group by third_id
                     ,third_title
                     ,sencond_id
                     ,sencond_title
                     ,first_id
                     ,first_title
		) t4
on 		t1.id = t4.third_id
;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值