same

same, selector

def process same(value: any): any

If all elements of a non-empty group are equal via ==, the selector same returns the sole value of the group. If the group is empty, the default value for the data type is used. Otherwise, when the group is non-empty and contains distinct elements, this selector fails.

Examples

table T = with
  [| as A, as B |]
  [| 1,   "a"   |]
  [| 1,   "a"   |]
  [| 2,   "b"   |]
  [| 2,   "b"   |]
  [| 3,   "c"   |]

table G[gdim] = by T.B

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

The above code results in the following table:

gdim same(T.A)
a 1
b 2
c 0

(Notice that the value for c is 0 – the default value for numbers.)

default can be used to specify the default value to use when the group is empty. For example, same(T.A) default 42 would result in the following table:

gdim same(T.A)
a 1
b 2
c 42

Errors

This selector fails when the group is non-empty and contains distinct elements.

See also

User Contributed Notes
0 notes + add a note