之前在工作中使用遇到的一些问题,下面把这些情况给简单分享一下,不多说,直接上问题。
1.问题背景
将上图数据效果转化为下图效果效果
manager |
["aa","aa","aa","bb","bb"] |
["cc","cc","dd"] |
["1","2","1","2","3"] |
manager |
aa,bb |
cc,dd |
1,2,3 |
2.实现思路
- 第一步使用lateral explode()函数侧视图打开manager列
- 第二步使用wm_concat()函数结合 group by 去重数据
3.代码实现
select approvalid
,subProductTag
,regexp_replace(wm_concat(distinct managers,','),'"','')as manager
from (
SELECT approvalid
,subProductTag
,managers
FROM (SELECT approvalid,subproducttag,manager FROM a WHERE ftime = %(dateFrom)s)tmp
lateral view explode(split(substr(manager,2,length(manager)-2),','))tmp1 as managers)
group by approvalid,subProductTag
4.总结
整体思路是使用先展开再聚合,如果有其他更好的实现方法,欢迎在下方评论区说出你的思路!