single

single(T.‘a) 🡒 ‘a, aggregator

The aggregator fails if any group has more than one element.

Example:

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

table G[gdim] = by T.B

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

The aggregator single succeeds on any empty group, and returns the default value for the given data type.

See also

single, contextual keyword

The contextual keyword single can be used as a modifier for the table creation through the keyword by. Using the single modifier allows values from the original table to be broadcast into the newly created table. When the modifier single is present, the table creation fails if any group has more than 1 element.

table T = with
  [| as Code |]
  [| "FR"    |]
  [| "US"    |]
  [| "UK"    |]
  [| "FR"    |] // Fails here due to duplicate

table Countries[c] = single by T.Code

show table "Countries" a1b3 with c

The broadcast behavior is illustrated by:

table T = with
  [| as Code, as X |]
  [| "FR",    1    |]
  [| "US",    2    |]
  [| "UK",    3    |]
  [| "DE",    4    |]

table Countries[c] = single by T.Code
Countries.X = T.X // broadcast allowed due to 'single' modifier
show table "" a1b3 with c, Countries.X

In the above script, removing the single modifier prevents the script from compiling as the broadcast Countries.X = T.X is rejected.

In practice, it is frequently expected that the single by construct would happen within a block filter that precisely removes the unintended duplicates from the original table.

table T = with
  [| as Code, as X |]
  [| "FR",    1    |]
  [| "US",    2    |]
  [| "UK",    3    |]
  [| "FR",    4    |]

where T.X != 4
  table Countries[c] = single by T.Code
  Countries.X = T.X 
  show table "" a1b3 with c, Countries.X