美文网首页
collect_list保持顺序

collect_list保持顺序

作者: 马尔克ov | 来源:发表于2019-06-18 16:20 被阅读0次

当需要collect_list(item_id) groupby user_id需要保持item_list的顺序时,用辅助列拼接item_id,sort_array做排序
lpad左补齐相同位数,保证字符串排序不会错

select device_id,
       regexp_replace(
         concat_ws(',',
           sort_array(
             collect_list(
               concat_ws(':',lpad(cast(click_order as string),5,'0'),cast(item_id as string))
             )
           )
         ),
       '\\d+\:','')
from user_click_history
where click_order >= 1 and click_order <= 1000
group by device_id;

参考博客

相关文章

网友评论

      本文标题:collect_list保持顺序

      本文链接:https://www.haomeiwen.com/subject/tanfqctx.html