如何替换数组中的元素?

时间:2019-03-27 11:57:01

标签: clickhouse

我需要将array(1,1,2,3,4,4,4,5,6,6,7)更改为select arrayMap (i-> (if(i=0, indexOf(arr,i)-1, i)), arr) from (select array(1,0,2,3,4,0,0,5,6,0,7) as arr)

我的代码:

array(1,1,2,3,4,1,1,5,6,1,7)

但是它返回{{1}}。怎么做?

1 个答案:

答案 0 :(得分:1)

看起来很奇怪,但是可以正常工作(备注:零索引的0值将被忽略):

SELECT
    arrayEnumerate(arr) AS indexes,
    [0, 1, 0, 2, 0, 0, 0, 3, 4, 0, 0, 5, 6, 0, 7, 8, 0] AS arr,
    arrayMap(i -> if((arr[i] = 0) AND (i > 1), arr[(i - arrayFirst(x -> (arr[(i - x)] != 0), indexes))], arr[i]), indexes) AS result
FORMAT Vertical

结果:

Row 1:
──────
indexes:    [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
sourceData: [0,1,0,2,0,0,0,3,4,0,0,5,6,0,7,8,0]
result:     [0,1,1,2,2,2,2,3,4,4,4,5,6,6,7,8,8]