Here’s how to use top() function.
Description:
Get the n smallest members from a sequence.
Syntax:
A.top(n,x) |
Calculates expression x over each member of the sequence and return a sequence consisting of the first n smallest values of x |
A.top(n;x,...) |
Calculates expression x over each member of the sequence and return a sequence consisting of the first n smallest members |
A.top(n,y,x) |
Loop members of sequence A to compute expression x, then loop the results to compute expression y, and finally return a sequence of x’s results that correspond to the top n smallest values of y. |
Note:
The function evaluates expression x with each member of the sequence A and returns the n smallest members. n must not be omitted. The omission of x is equivalent to ~.
Parameter:
A |
A sequence |
n |
An integer |
y |
An expression |
x |
An expression |
Option:
@1 |
Return a single value if n is ±1; and null if n is 0 |
@2 |
Calculate the concatenation of all sequence-type members in sequence A and get the first n smallest members from the new sequence |
@0 |
Do not ignore null members |
Return value:
Sequence
Example:
|
A |
|
1 |
[a,c,e,g,f,d,b] |
|
2 |
=A1.top(3,~) |
|
3 |
=A1.top(3) |
Same as A2, when x is omitted |
4 |
=demo.query("select * from EMPLOYEE") |
|
5 |
=A4.top(10,SALARY) |
Get the salaries of 10 employees who are the lowest paid |
6 |
=A4.top(10;SALARY) |
Get the records of 10 employees who are the lowest paid |
7 |
[21,3,12,5] |
|
8 |
=A6.top(3,~%10,~+6) |
[11,27,18] |
9 |
=A7.top(-1) |
|
10 |
=A7.top@1(-1) |
|
11 |
=A4.top(10;SALARY,EID) |
Get top 10 employee records where SALARY is the lowest and EID is the smallest |
12 |
[12,5,-6] |
|
13 |
[36,1,15,9,85] |
|
14 |
=[A12,A13].top@2(2) |
Concatenate A12 and A13 and get the first two smallest values |
15 |
=[1,5,1,7,2,null,4,3] .top@0(3) |
[null,1,1] |
Related function: