对数组向量的每行元素进行去重

想问下,怎么对b进行去重,官方的 distinct 不能在 group by,context by 中使用,而且使用 distinct 函数还不能保序。只能 ungroup 然后用 isDuplicated 删除么?

attachments-2023-11-Z0XqNXso654353d231f39.jpg

请先 登录 后评论

2 个回答

Polly

可以用 loop 函数实现,参考脚本如下

t = table(`a`b as sym, array(INT[]).append!([1 2 1, 2 3 2]) as val)
select sym, loop(distinct,val) as`val from t // 不保证顺序
select sym, loop(x -> x[ifirstHit{==,x,}:E(distinct x).sort()], val) as `val from t // 保证顺序
请先 登录 后评论
SaintM

假设有含arrayVector列的表t,要求对arrayVector列中每一行的数据做去重。

t = table(`a`b as sym, array(INT[]).append!([1 2 1 3, 2 3 2 1 0 0]) as val)

attachments-2024-01-2qnXMbvg65b3885db4af1.png方案一:loop + distinct 对arrayVector列中每一行的数据做去重。该方案去重后每行的元素顺序不会保留。

t2 = select sym, array(INT[]).append!(loop(distinct,val))as `val from t

attachments-2024-01-LJyJT1Al65b38880a77d5.png方案二:loop + distinct 去重,并使用ifirstHit 使结果按照去重之前每个值第一次出现的顺序排序。

t3 = select sym, array(INT[]).append!(loop(x -> x[ifirstHit{==,x,}:E(distinct x).sort()], val)) as `val from t

attachments-2024-01-GAo2eOC065b38898e45ff.png

请先 登录 后评论
  • 2 关注
  • 0 收藏,432 浏览
  • veryOrdinary 提出于 2023-11-02 15:46

相似问题