ClickHouse/docs/en/sql-reference/aggregate-functions/reference/grouparraysorted.md

48 lines
1.1 KiB
Markdown
Raw Normal View History

2021-12-29 15:56:58 +00:00
---
sidebar_position: 108
2021-12-29 15:56:58 +00:00
---
2022-02-08 22:08:44 +00:00
# groupArraySorted {#groupArraySorted}
2021-12-29 15:56:58 +00:00
Returns an array with the first N items in ascending order.
``` sql
2022-02-08 22:08:44 +00:00
groupArraySorted(N)(column)
2021-12-29 15:56:58 +00:00
```
**Arguments**
- `N` The number of elements to return.
If the parameter is omitted, default value 10 is used.
**Arguments**
- `column` The value.
- `expr` — Optional. The field or expresion to sort by. If not set values are sorted by themselves.
2021-12-29 15:56:58 +00:00
**Example**
Gets the first 10 numbers:
``` sql
2022-02-08 22:08:44 +00:00
SELECT groupArraySorted(10)(number) FROM numbers(100)
2021-12-29 15:56:58 +00:00
```
``` text
2022-02-08 22:08:44 +00:00
┌─groupArraySorted(10)(number)─┐
2021-12-29 15:56:58 +00:00
│ [0,1,2,3,4,5,6,7,8,9] │
└──────────────────────────────┘
```
Or the last 10:
``` sql
2022-02-08 22:08:44 +00:00
SELECT groupArraySorted(10)(number, -number) FROM numbers(100)
2021-12-29 15:56:58 +00:00
```
``` text
2022-02-08 22:08:44 +00:00
┌─groupArraySorted(10)(number, negate(number))─┐
2021-12-29 15:56:58 +00:00
│ [99,98,97,96,95,94,93,92,91,90] │
└──────────────────────────────────────────────┘
```