whichever

whichever, selector

There are other concepts named whichever.
See the entire list.

def process whichever(value: any): any

The selector returns one value for each group, if the group is not empty. If the group is empty, the default value for the data type is used.

Examples

table T = with
  [| as A, as B |]
  [| "hello1", "a" |]
  [| "hello2", "a" |]
  [| "hello3", "b" |]
  [| "world1", "b" |]
  [| "world2", "c" |]

table G[gdim] = by T.B

where T.B != "c"
  show table "" a1b4 with
    gdim
    whichever(T.A)
    group by gdim

The above code may result in the following table (however, see the remarks section):

gdim whichever(T.A)
a hello1
b hello3
c

(Notice that the value for c is the empty string – the default value for strings.)

default can be used to specify the default value to use when the group is empty. For example, whichever(T.A) default "x" may result in the following table:

gdim whichever(T.A)
a hello1
b hello3
c x

Remarks

Whereas whichever is a completely deterministic aggregator (i.e., running the script twice over the same data yields the same result), the ordering of the data should not be relied upon. In other words, changing the script or upgrading to a newer version of Envision can alter the result of whichever.

whichever cannot be used with sort.

Recipes and best practices

This aggregator is intended as a faster alternative to first or last, for situations where an ordering of data elements is not essential by design.

See also

User Contributed Notes
0 notes + add a note