Skip to main content

groupArraySample

Creates an array of sample argument values. The size of the resulting array is limited to max_size elements. Argument values are selected and added to the array randomly.

Syntax

groupArraySample(max_size[, seed])(x)

Arguments

  • max_size β€” Maximum size of the resulting array. UInt64.
  • seed β€” Seed for the random number generator. Optional. UInt64. Default value: 123456.
  • x β€” Argument (column name or expression).

Returned values

  • Array of randomly selected x arguments.

Type: Array.

Examples

Consider table colors:

β”Œβ”€id─┬─color──┐
β”‚ 1 β”‚ red β”‚
β”‚ 2 β”‚ blue β”‚
β”‚ 3 β”‚ green β”‚
β”‚ 4 β”‚ white β”‚
β”‚ 5 β”‚ orange β”‚
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Query with column name as argument:

SELECT groupArraySample(3)(color) as newcolors FROM colors;

Result:

β”Œβ”€newcolors──────────────────┐
β”‚ ['white','blue','green'] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Query with column name and different seed:

SELECT groupArraySample(3, 987654321)(color) as newcolors FROM colors;

Result:

β”Œβ”€newcolors──────────────────┐
β”‚ ['red','orange','green'] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Query with expression as argument:

SELECT groupArraySample(3)(concat('light-', color)) as newcolors FROM colors;

Result:

β”Œβ”€newcolors───────────────────────────────────┐
β”‚ ['light-blue','light-orange','light-green'] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜