# `Ash.Query.Aggregate`
[🔗](https://github.com/ash-project/ash/blob/v3.23.1/lib/ash/query/aggregate.ex#L5)

Represents an aggregated association value

# `kind`

```elixir
@type kind() ::
  :custom | :exists | :avg | :min | :max | :list | :sum | :first | :count
```

# `t`

```elixir
@type t() :: %Ash.Query.Aggregate{
  agg_name: term(),
  authorize?: term(),
  constraints: term(),
  context: term(),
  default_value: term(),
  field: term(),
  filterable?: term(),
  implementation: term(),
  include_nil?: term(),
  join_filters: term(),
  kind: term(),
  load: term(),
  multitenancy: term(),
  name: term(),
  query: term(),
  read_action: term(),
  related?: term(),
  relationship_path: term(),
  resource: term(),
  sensitive?: term(),
  sortable?: term(),
  type: term(),
  uniq?: term()
}
```

# `default_value`

# `new`

Create a new aggregate, used with `Query.aggregate` or `Ash.aggregate`

### Options

* `:path` (list of `t:atom/0`) - The relationship path to aggregate over. Only used when adding aggregates to a query. The default value is `[]`.

* `:query` (`t:term/0`) - A base query to use for the aggregate, or a keyword list to be passed to `Ash.Query.build/2`

* `:field` - The field to use for the aggregate. Not necessary for all aggregate types.

* `:expr` (`t:term/0`) - An expression to aggregate, cannot be used with `field`.

* `:expr_type` - The type of the expression, required if `expr` is used.

* `:arguments` (`t:map/0`) - Arguments to pass to the field, if field is a calculation.

* `:default` (`t:term/0`) - A default value to use for the aggregate if it returns `nil`.

* `:filterable?` (`t:boolean/0`) - Whether or not this aggregate may be used in filters. The default value is `true`.

* `:sortable?` (`t:boolean/0`) - Whether or not this aggregate may be used in sorts. The default value is `true`.

* `:type` (`t:term/0`) - A type to use for the aggregate.

* `:constraints` (`t:term/0`) - Type constraints to use for the aggregate. The default value is `[]`.

* `:implementation` (`t:term/0`) - The implementation for any custom aggregates.

* `:read_action` (`t:atom/0`) - The read action to use for the aggregate, defaults to the primary read action.

* `:uniq?` (`t:boolean/0`) - Whether or not to only consider unique values. Only relevant for `count` and `list` aggregates. The default value is `false`.

* `:include_nil?` (`t:boolean/0`) - Whether or not to include `nil` values in the aggregate. Only relevant for `list` and `first` aggregates. The default value is `false`.

* `:join_filters` (map of one or a list of `t:atom/0` keys and `t:term/0` values) - A map of relationship paths (an atom or list of atoms), to an expression to apply when fetching the aggregate data. See the aggregates guide for more. The default value is `%{}`.

* `:sensitive?` (`t:boolean/0`) - Whether or not references to this aggregate will be considered sensitive The default value is `false`.

* `:tenant` (`t:term/0`) - The tenant to use for the aggregate, if applicable. The default value is `nil`.

* `:multitenancy` - Configures multitenancy behavior for the aggregate.  
  * `:bypass` - Aggregate data across all tenants, ignoring the tenant context even if it's set.
   Valid values are :bypass

* `:authorize?` (`t:boolean/0`) - Whether or not the aggregate query should authorize based on the target action.  
  See `d:Ash.Resource.Dsl.aggregates|count` for more information. The default value is `true`.

* `:resource` (`t:atom/0`) - For unrelated aggregates, the target resource to aggregate over

* `:related?` (`t:boolean/0`) - Whether this aggregate uses a relationship path (true) or a direct resource reference (false) The default value is `true`.

# `new!`

Create a new aggregate, used with `Query.aggregate` or `Ash.aggregate`, raising on errors.

See `new/4` for more information.

### Options

* `:path` (list of `t:atom/0`) - The relationship path to aggregate over. Only used when adding aggregates to a query. The default value is `[]`.

* `:query` (`t:term/0`) - A base query to use for the aggregate, or a keyword list to be passed to `Ash.Query.build/2`

* `:field` - The field to use for the aggregate. Not necessary for all aggregate types.

* `:expr` (`t:term/0`) - An expression to aggregate, cannot be used with `field`.

* `:expr_type` - The type of the expression, required if `expr` is used.

* `:arguments` (`t:map/0`) - Arguments to pass to the field, if field is a calculation.

* `:default` (`t:term/0`) - A default value to use for the aggregate if it returns `nil`.

* `:filterable?` (`t:boolean/0`) - Whether or not this aggregate may be used in filters. The default value is `true`.

* `:sortable?` (`t:boolean/0`) - Whether or not this aggregate may be used in sorts. The default value is `true`.

* `:type` (`t:term/0`) - A type to use for the aggregate.

* `:constraints` (`t:term/0`) - Type constraints to use for the aggregate. The default value is `[]`.

* `:implementation` (`t:term/0`) - The implementation for any custom aggregates.

* `:read_action` (`t:atom/0`) - The read action to use for the aggregate, defaults to the primary read action.

* `:uniq?` (`t:boolean/0`) - Whether or not to only consider unique values. Only relevant for `count` and `list` aggregates. The default value is `false`.

* `:include_nil?` (`t:boolean/0`) - Whether or not to include `nil` values in the aggregate. Only relevant for `list` and `first` aggregates. The default value is `false`.

* `:join_filters` (map of one or a list of `t:atom/0` keys and `t:term/0` values) - A map of relationship paths (an atom or list of atoms), to an expression to apply when fetching the aggregate data. See the aggregates guide for more. The default value is `%{}`.

* `:sensitive?` (`t:boolean/0`) - Whether or not references to this aggregate will be considered sensitive The default value is `false`.

* `:tenant` (`t:term/0`) - The tenant to use for the aggregate, if applicable. The default value is `nil`.

* `:multitenancy` - Configures multitenancy behavior for the aggregate.  
  * `:bypass` - Aggregate data across all tenants, ignoring the tenant context even if it's set.
   Valid values are :bypass

* `:authorize?` (`t:boolean/0`) - Whether or not the aggregate query should authorize based on the target action.  
  See `d:Ash.Resource.Dsl.aggregates|count` for more information. The default value is `true`.

* `:resource` (`t:atom/0`) - For unrelated aggregates, the target resource to aggregate over

* `:related?` (`t:boolean/0`) - Whether this aggregate uses a relationship path (true) or a direct resource reference (false) The default value is `true`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
