Nth_element
Aggregation functions nth_element
description
This operator returns the value of the nth element of ranked attribute a, which can be done in partition.
syntax
- nth_element(a, n)
- nth_element(a, n, relation)
definition
- nth_element(a, n) results in a parameter with the nth element of the ascending sorted attribute a.
- nth_element(a, n, relation) results in an Attribute with the nth elements of the ascending sorted attribute a, grouped by relation. The Domain unit of the resulting attribute is the Values unit of the relation.
applies to
- attribute a with numeric (uint2, uint4, uint8, uint32, float32, or bool Value type)
- attribute n with uint32 value type
- relation with Value type of the group CanBeDomainUnit
conditions
- The values unit of the resulting Data item should match with regard to value type and Metric with the values unit of attribute a
- The domain of Argument a and relation must match,
- The domain of argument n must be the same as the resulting domain or n must be a parameter.
since version
5.61
performance
- Without relation: O(n log n) where n = number of elements in the domain of a. Requires sorting.
- With relation: O(n log k) on average where n = total elements and k = average partition size. Uses partial sorting per partition.
example
1. parameter<uint32> nth_elementNrInh := nth_element(City/NrInhabitants, 2); result = 300
2. attribute<float32> nth_elementNrInhRegion (Region) :=
nth_element(
City/NrInhabitants
, 0
, City/Region_rel
);
| City/NrInhabitants | City/Region_rel |
|---|---|
| 550 | 0 |
| 525 | 1 |
| 300 | 2 |
| 500 | 1 |
| 200 | 3 |
| 175 | null |
| null | 3 |
domain City, nr of rows = 7
| nth_elementNrInhRegion |
|---|
| 550 |
| 500 |
| 300 |
| 200 |
| null |
domain Region, nr of rows = 5