ClickHouse/docs/en/sql-reference/aggregate-functions/reference/groupsortedarray.md
2022-01-27 12:22:59 +01:00

1.1 KiB
Raw Blame History

toc_priority
108

groupSortedArray

Returns an array with the first N items in ascending order.

groupSortedArray(N)(column)

Arguments

  • N The number of elements to return.

If the parameter is omitted, default value 10 is used.

Arguments

  • x The value.
  • expr — Optional. The field or expresion to sort by. If not set values are sorted by themselves. Integer.

Example

Gets the first 10 numbers:

SELECT groupSortedArray(10)(number) FROM numbers(100)
┌─groupSortedArray(10)(number)─┐
│ [0,1,2,3,4,5,6,7,8,9]        │
└──────────────────────────────┘

Or the last 10:

SELECT groupSortedArray(10)(number, -number) FROM numbers(100)
┌─groupSortedArray(10)(number, negate(number))─┐
│ [99,98,97,96,95,94,93,92,91,90]              │
└──────────────────────────────────────────────┘