ClickHouse commonly used higher-order functions

Insert picture description here

One, de-duplication

1.arrayDistinct

Deduplicate the array

SELECT arrayDistinct( [1,2,3,6,34,3,11])

2.arrayUniq

Count how many unique values ​​are in the array

SELECT arrayUniq( [1,2,3,6,34,3,11])

3.arrayCompact

Implement adjacent deduplication of data in the array

SELECT arrayCompact([1, 2, 2, 3, 2, 3, 3])

Two, array attributes

1.arrayJoin

Expand the array, change rows into columns

SELECT arrayJoin( [1,2,3,6,34,3,11] ) as a

2.arrayFilter

Filter out the data that meets the conditions in the array

SELECT a from (SELECT arrayFilter(x->x%2=0, [1,2,3,6,34,3,11]) as a

3.arrayEnumerate

Return array subscript

SELECT arrayEnumerate([1,2,3,6,34,3,11])

4.arrayReduce

Perform aggregation operations on the array, such as min, max, avg, etc.

SELECT arrayReduce('avg', [1,2,3,6,34,3,11] )

5.arrayEnumerateDense

Mark out the same elements in the array

SELECT arrayEnumerateDense( [1,2,3,6,34,3,11] )

6.hasAny

Determine whether the array contains any one of the values, if it contains, it returns 1, otherwise it returns 0

SELECT hasAny( [1,2,3,6,34,3,11] , [3,4])

7.hasAll

Determine whether the array contains all the values ​​in some values, if it contains, it returns 1, otherwise it returns 0

SELECT hasAll( [1,2,3,6,34,3,11] , [3,4])

8.arrayWithConstant

Generate an array of specified length

SELECT arrayWithConstant( 3, 'a')

Three, cutting / splicing

1.arrayStringConcat

The array elements are spliced ​​according to the given separator, and the spliced ​​string is returned (array elements must be String type)

SELECT arrayStringConcat( ['2020','12','19'], '-') 

2.arraySlice

Cut the array. The next two parameters are the cutting position and the number of segments after cutting.

SELECT arraySlice( [1,2,3,6,34,3,11],-3,2)

Fourth, sort

1.arraySort

Ascending array

SELECT a from (SELECT arraySort([1,2,3,6,34,3,11]) as a)

2.arrayReverseSort

Descending the array

SELECT a from (SELECT arrayReverseSort([1,2,3,6,34,3,11]) as a)

Five, add / delete the first and last elements

1.arrayPushFront

Add an element to the first position of the array

SELECT arrayPushFront( [1,2,3,6,34,3,11] , 8)

2.arrayPushBack

Add elements to the end of the array

SELECT arrayPushBack( [1,2,3,6,34,3,11] , 8)

3.arrayPopFront

Delete the first element in the array

SELECT arrayPopFront( [1,2,3,6,34,3,11] )

4.arrayPopBack

Delete the last element in the array

SELECT arrayPopBack( [1,2,3,6,34,3,11] )

Six, calculate the difference

1.arrayDifference

Calculate the difference between the two values ​​in the array, this bit = current-the former (not including the first number, the first bit of the result is 0 by default)

SELECT arrayDifference( [1,2,3,6,34,3,11] )

2.runningDifference

Calculate the difference between the values ​​before and after a column, the digit = current-the former (not including the first number, the first digit of the result is 0 by default)

select a,runningDifference(a)  from (SELECT arrayJoin( [1,2,3,6,34,3,11]) as a)

Guess you like

Origin blog.csdn.net/Bertil/article/details/111411873