FirstSortedValue - script function
FirstSortedValue() returns the value from the expression specified in value that corresponds to the result of sorting the sort_weight argument, taking into account rank, if specified. If more than one resulting value shares the same sort_weight for the specified rank, the function returns NULL.
The sorted values are iterated over a number of records, as defined by a group by clause, or aggregated across the full data set if no group by clause is defined.
Syntax:
Return data type: dual
Arguments:
Argument | Description |
---|---|
|
The function finds the value of the expression value that corresponds to the result of sorting sort_weight. |
|
The expression containing the data to be sorted. The first (lowest) value of sort_weight is found, from which the corresponding value of the value expression is determined. If you place a minus sign in front of sort_weight, the function returns the last (highest) sorted value instead. |
|
By stating a rank "n" larger than 1, you get the nth sorted value. |
|
If the word DISTINCT occurs before the function arguments, duplicates resulting from the evaluation of the function arguments are disregarded. |
Examples and results:
Add the example script to your document and run it. Then add, at least, the fields listed in the results column to a sheet in our document to see the result.
Example | Result |
---|---|
LOAD * inline [ Customer|Product|OrderNumber|UnitSales|CustomerID Astrida|AA|1|10|1 Astrida|AA|7|18|1 Astrida|BB|4|9|1 Astrida|CC|6|2|1 Betacab|AA|5|4|2 Betacab|BB|2|5|2 Betacab|DD|12|25|2 Canutility|AA|3|8|3 Canutility|CC|13|19|3 Divadip|AA|9|16|4 Divadip|AA|10|16|4 Divadip|DD|11|10|4 ] (delimiter is '|');
FirstSortedValue: LOAD Customer,FirstSortedValue(Product, UnitSales) as MyProductWithSmallestOrderByCustomer Resident Temp Group By Customer; |
Customer MyProductWithSmallestOrderByCustomer Astrida CC Betacab AA Canutility AA Divadip DD The function sorts Because |
Given that the LOAD Customer,FirstSortedValue(Product, -UnitSales) as MyProductWithLargestOrderByCustomer Resident Temp Group By Customer; |
Customer MyProductWithLargestOrderByCustomer Astrida AA Betacab DD Canutility CC Divadip - A minus sign precedes the Because |
Given that the LOAD Customer,FirstSortedValue(distinct Product, -UnitSales) as MyProductWithSmallestOrderByCustomer Resident Temp Group By Customer; |
Customer MyProductWithLargestOrderByCustomer Astrida AA Betacab DD Canutility CC Divadip AA This is the same as the previous example, except the |