A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://clickhouse.com/docs/sql-reference/aggregate-functions/parametric-functions below:

Parametric Aggregate Functions | ClickHouse Docs

Parametric aggregate functions

Some aggregate functions can accept not only argument columns (used for compression), but a set of parameters – constants for initialization. The syntax is two pairs of brackets instead of one. The first is for parameters, and the second is for arguments.

histogram

Calculates an adaptive histogram. It does not guarantee precise results.

The functions uses A Streaming Parallel Decision Tree Algorithm. The borders of histogram bins are adjusted as new data enters a function. In common case, the widths of bins are not equal.

Arguments

valuesExpression resulting in input values.

Parameters

number_of_bins — Upper limit for the number of bins in the histogram. The function automatically calculates the number of bins. It tries to reach the specified number of bins, but if it fails, it uses fewer bins.

Returned values

Example

You can visualize a histogram with the bar function, for example:

In this case, you should remember that you do not know the histogram bin borders.

sequenceMatch

Checks whether the sequence contains an event chain that matches the pattern.

Syntax

Note

Events that occur at the same second may lay in the sequence in an undefined order affecting the result.

Arguments

Parameters

Returned values

Type: UInt8.

Pattern syntax

Examples

Consider data in the t table:

Perform the query:

The function found the event chain where number 2 follows number 1. It skipped number 3 between them, because the number is not described as an event. If we want to take this number into account when searching for the event chain given in the example, we should make a condition for it.

In this case, the function couldn't find the event chain matching the pattern, because the event for number 3 occurred between 1 and 2. If in the same case we checked the condition for number 4, the sequence would match the pattern.

See Also

sequenceCount

Counts the number of event chains that matched the pattern. The function searches event chains that do not overlap. It starts to search for the next chain after the current chain is matched.

Note

Events that occur at the same second may lay in the sequence in an undefined order affecting the result.

Syntax

Arguments

Parameters

Returned values

Type: UInt64.

Example

Consider data in the t table:

Count how many times the number 2 occurs after the number 1 with any amount of other numbers between them:

sequenceMatchEvents

Return event timestamps of longest event chains that matched the pattern.

Note

Events that occur at the same second may lay in the sequence in an undefined order affecting the result.

Syntax

Arguments

Parameters

Returned values

Type: Array.

Example

Consider data in the t table:

Return timestamps of events for longest chain

See Also

windowFunnel

Searches for event chains in a sliding time window and calculates the maximum number of events that occurred from the chain.

The function works according to the algorithm:

Syntax

Arguments

Parameters

Returned value

The maximum number of consecutive triggered conditions from the chain within the sliding time window. All the chains in the selection are analyzed.

Type: Integer.

Example

Determine if a set period of time is enough for the user to select a phone and purchase it twice in the online store.

Set the following chain of events:

  1. The user logged in to their account on the store (eventID = 1003).
  2. The user searches for a phone (eventID = 1007, product = 'phone').
  3. The user placed an order (eventID = 1009).
  4. The user made the order again (eventID = 1010).

Input table:

Find out how far the user user_id could get through the chain in a period in January-February of 2019.

Query:

Result:

retention

The function takes as arguments a set of conditions from 1 to 32 arguments of type UInt8 that indicate whether a certain condition was met for the event. Any condition can be specified as an argument (as in WHERE).

The conditions, except the first, apply in pairs: the result of the second will be true if the first and second are true, of the third if the first and third are true, etc.

Syntax

Arguments

Returned value

The array of 1 or 0.

Type: UInt8.

Example

Let's consider an example of calculating the retention function to determine site traffic.

1. Create a table to illustrate an example.

Input table:

Query:

Result:

2. Group users by unique ID uid using the retention function.

Query:

Result:

3. Calculate the total number of site visits per day.

Query:

Result:

Where:

uniqUpTo(N)(x)

Calculates the number of different values of the argument up to a specified limit, N. If the number of different argument values is greater than N, this function returns N + 1, otherwise it calculates the exact value.

Recommended for use with small Ns, up to 10. The maximum value of N is 100.

For the state of an aggregate function, this function uses the amount of memory equal to 1 + N * the size of one value of bytes. When dealing with strings, this function stores a non-cryptographic hash of 8 bytes; the calculation is approximated for strings.

For example, if you had a table that logs every search query made by users on your website. Each row in the table represents a single search query, with columns for the user ID, the search query, and the timestamp of the query. You can use uniqUpTo to generate a report that shows only the keywords that produced at least 5 unique users.

uniqUpTo(4)(UserID) calculates the number of unique UserID values for each SearchPhrase, but it only counts up to 4 unique values. If there are more than 4 unique UserID values for a SearchPhrase, the function returns 5 (4 + 1). The HAVING clause then filters out the SearchPhrase values for which the number of unique UserID values is less than 5. This will give you a list of search keywords that were used by at least 5 unique users.

sumMapFiltered

This function behaves the same as sumMap except that it also accepts an array of keys to filter with as a parameter. This can be especially useful when working with a high cardinality of keys.

Syntax

sumMapFiltered(keys_to_keep)(keys, values)

Parameters

Returned Value

Example

Query:

Result:

sumMapFilteredWithOverflow

This function behaves the same as sumMap except that it also accepts an array of keys to filter with as a parameter. This can be especially useful when working with a high cardinality of keys. It differs from the sumMapFiltered function in that it does summation with overflow - i.e. returns the same data type for the summation as the argument data type.

Syntax

sumMapFilteredWithOverflow(keys_to_keep)(keys, values)

Parameters

Returned Value

Example

In this example we create a table sum_map, insert some data into it and then use both sumMapFilteredWithOverflow and sumMapFiltered and the toTypeName function for comparison of the result. Where requests was of type UInt8 in the created table, sumMapFiltered has promoted the type of the summed values to UInt64 to avoid overflow whereas sumMapFilteredWithOverflow has kept the type as UInt8 which is not large enough to store the result - i.e. overflow has occurred.

Query:

Result:

sequenceNextNode

Returns a value of the next event that matched an event chain.

Experimental function, SET allow_experimental_funnel_functions = 1 to enable it.

Syntax

Parameters

Arguments

Returned values

Type: Nullable(String).

Example

It can be used when events are A->B->C->D->E and you want to know the event following B->C, which is D.

The query statement searching the event following A->B:

Result:

Behavior for forward and head

Behavior for backward and tail

Behavior for forward and first_match

Behavior for backward and last_match

Behavior for base_condition


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4