clickhouse之函数(二)数组

array

arrayElement(arr, n), operator arr[n]

    从array中获取索引为n的元素,索引从1开始,支持负数,-1指最后一位;索引超过大小返回类型的默认值

has(arr, elem)

    arr中是否有elem元素,0无1有

hasAll(set, subset)

     检查arr是否是另一个arr的子集;0无1有,空arr是如何arr的子集,null是一个值,数组中元素顺序无关紧要

hasAny(arr1,arr2)

     数组是否有交集,0无1有,null是一个值,元素顺序无关

countEqual(arr, x)

    返回等于x的arr中元素的个数,null独立值处理,

arrayEnumerate(arr)

    通常与array join一起使用。它允许在数据array join后对每个数组只计数一次

SELECT
    count() AS Reaches,
    countIf(num = 1) AS Hits
FROM test.hits
ARRAY JOIN
    GoalsReached,
    arrayEnumerate(GoalsReached) AS num
WHERE CounterID = 160656
LIMIT 10
SELECT
    sum(length(GoalsReached)) AS Reaches,
    count() AS Hits
FROM test.hits
WHERE (CounterID = 160656) AND notEmpty(GoalsReached)
reach为转换次数(应用数组连接后接收的字符串) Hits为页面浏览量(数组连接前的字符串)

arrayEnumerateUniq(arr, ...)?

  返回与源数组相同大小的数组,指示每个元素在具有相同值的元素中的位置

Returns an array the same size as the source array, indicating for each element what its position is among elements with the same value. For example: arrayEnumerateUniq([10, 20, 10, 30]) = [1, 1, 2, 1].

This function is useful when using ARRAY JOIN and aggregation of array elements. Example:

SELECT
    Goals.ID AS GoalID,
    sum(Sign) AS Reaches,
    sumIf(Sign, num = 1) AS Visits
FROM test.visits
ARRAY JOIN
    Goals,
    arrayEnumerateUniq(Goals.ID) AS num
WHERE CounterID = 160656
GROUP BY GoalID
ORDER BY Reaches DESC
LIMIT 10
┌──GoalID─┬─Reaches─┬─Visits─┐
│   53225 │    3214 │   1097 │
│ 2825062 │    3188 │   1097 │
│   56600 │    2803 │    488 │
│ 1989037 │    2401 │    365 │
│ 2830064 │    2396 │    910 │
│ 1113562 │    2372 │    373 │
│ 3270895 │    2262 │    812 │
│ 1084657 │    2262 │    345 │
│   56599 │    2260 │    799 │
│ 3271094 │    2256 │    812 │
└─────────┴─────────┴────────┘
In this example, each goal ID has a calculation of the number of conversions (each element in the Goals nested data structure is a goal that was reached, which we refer to as a conversion) and the number of sessions. Without ARRAY JOIN, we would have counted the number of sessions as sum(Sign). But in this particular case, the rows were multiplied by the nested Goals structure, so in order to count each session one time after this, we apply a condition to the value of the arrayEnumerateUniq(Goals.ID) function.

The arrayEnumerateUniq function can take multiple arrays of the same size as arguments. In this case, uniqueness is considered for tuples of elements in the same positions in all the arrays.

SELECT arrayEnumerateUniq([1, 1, 1, 2, 2, 2], [1, 1, 2, 1, 1, 2]) AS res
┌─res───────────┐
│ [1,2,1,1,2,1] │
└───────────────┘
This is necessary when using ARRAY JOIN with a nested data structure and further aggregation across multiple elements in this structure.

arrayPopBack(array)移除最后一位;arrayPopFront(array)移除第一位

arrayPushBack(array,single_value)尾部添加原色,类型,添加null时,数组元素类型转为nullable

arrayPushFront(array,single_value)

arrayResize(array, size[, extender])改变数组长度,size小于原数组 从右边开始截,长用extender或默认值填充,返回数组

arraySlice(array, offset[, length])返回数组,offset值1从左边开始,负数右边,length负数[offset, array_length - length),省略返回offset开始到最后的元素数组

arrayUniq(arr,…)计算数组中不同元素的个数,如传递了多个参数,则计算多个数组中对应位置的不同元组元素的数量

分割合并

splitByChar(separator,s)

splitByString(separator, s)、arrayStringConcat(arr[, separator]) 分隔符默认为空,返回字符串

alphaTokens(s)从a-z和A-Z中返回连续字节 SELECT alphaTokens('abca1abc')  ['abca','abc']

发布了431 篇原创文章 · 获赞 155 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/ma15732625261/article/details/86626016