2021-12-29 15:56:58 +00:00
|
|
|
|
---
|
2022-04-09 13:29:05 +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**
|
|
|
|
|
|
2022-02-04 14:53:27 +00:00
|
|
|
|
- `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] │
|
|
|
|
|
└──────────────────────────────────────────────┘
|
|
|
|
|
```
|