Hive SQL: 拆分并展开 tsh_user_id 字段
该 SQL 代码使用 LATERAL VIEW 和 explode 函数将 tsh_user_id 字段按逗号分隔并展开成多行。
if(length(tsh_user_id))=0,'0,0', tsh_user_id): 判断 tsh_user_id 字段的长度,如果长度为 0,则用 '0,0' 替换该字段,否则保留原值。split(..., ','): 使用逗号作为分隔符,将上述结果拆分成数组。explode(...) te as chips_policy_openid1: 使用 explode 函数将数组展开成多行,并将每行的值赋给 chips_policy_openid1 变量。
代码解析:
LATERAL VIEW explode(split(if(length(tsh_user_id))=0,'0,0', tsh_user_id), ',')) te as chips_policy_openid1
示例:
假设 tsh_user_id 字段的值为 '123,456',则执行上述 SQL 代码后,会生成两行数据,chips_policy_openid1 的值为 '123' 和 '456'。
如果 tsh_user_id 字段的值为空,则执行上述 SQL 代码后,会生成两行数据,chips_policy_openid1 的值为 '0' 和 '0'。
原文地址: https://www.cveoy.top/t/topic/nalP 著作权归作者所有。请勿转载和采集!