any

any, aggregator

def nosort process any(value: boolean): boolean default false

Returns true iff at least one boolean value in the group value is true. If the group is empty, returns false.

Examples

Basic usage

table T = with
  [| as Id, as HasError |]
  [| "a",   false       |]
  [| "b",   true        |]
  [| "c",   false       |]

hasAnyError = any(T.HasError)

// true
show scalar "Any errors?" with hasAnyError

Grouping by category

table Products = with
  [| as Category, as Product, as HasDiscount |]
  [| "fruit",     "apple",    false          |]
  [| "fruit",     "banana",   true           |]
  [| "fruit",     "cherry",   false          |]
  [| "veg",       "carrot",   false          |]
  [| "veg",       "potato",   false          |]

table Categories[cat] = by Products.Category

Categories.HasAnyDiscount = any(Products.HasDiscount)

show table "Discount status by category" with
  cat
  Categories.HasAnyDiscount

This produces the following table:

cat HasAnyDiscount
fruit True
veg False

With empty groups

table T = with
  [| as Category, as HasIssue |]
  [| "A",         false       |]
  [| "A",         false       |]
  [| "B",         true        |]
  [| "C",         false       |]

table Categories = with
  [| as Category |]
  [| "A" |]
  [| "B" |]
  [| "C" |]
  [| "D" |] // No data for this category.

Categories.HasAnyIssue = any(T.HasIssue) by T.Category at Categories.Category

show table "Issue detection" with
  Categories.Category
  Categories.HasAnyIssue

This produces:

Category HasAnyIssue
A False
B True
C False
D False

Remarks

The any aggregator is marked as nosort because the order of evaluation does not matter (mathematically) for a logical OR operation.

Recipes and best practices

  1. Exception detection: You can use any to check if any record in a dataset has an exceptional condition:
    hasNegativeStock = any(Items.Quantity < 0)
  2. Conditional checks with filtering: Combine with filters to check conditions on specific subsets:
    hasActivePromotion = any(Items.Discount > 0) when Items.IsActive
  3. Existence check: You can use any to determine if entities exist matching certain criteria:
    hasPremiumCustomers = any(Customers.Tier == "Premium")
  4. Group-based existence: You can use any with a by clause to identify groups containing specific conditions:
    Categories.HasPromotion = any(Items.OnPromotion) by Items.Category
  5. Negation of all: any(not X) is mathematically equivalent to not (all X), although the latter one is computationally more efficient.

See also

User Contributed Notes
0 notes + add a note