# Aggregations

The matches of a condition's main matcher can be aggregated based on another field's values. Those values are aggregated using a specified [operator](/rules-engine/operators.md) and the results of the aggregation is compared against a provided value using the aggregation's matcher to add additional restrictions and define the overall aggregated condition's matches.

<table><thead><tr><th width="218">Key</th><th width="140">Type<select multiple><option value="E0Cs57JSKyBG" label="Integer" color="blue"></option><option value="ksO0FRw4oBgS" label="Boolean" color="blue"></option><option value="XJ81bAWrKRl1" label="String" color="blue"></option><option value="bMMJFACVlMos" label="Array" color="blue"></option><option value="bn7L3K4qyUDV" label="Object" color="blue"></option></select></th><th width="120" data-type="checkbox">Required</th><th width="277">Description</th><th>Example</th></tr></thead><tbody><tr><td><strong><code>field</code></strong></td><td><span data-option="XJ81bAWrKRl1">String</span></td><td>true</td><td>The resource's field on which to perform the aggregation (expressed in dot notation). Must be an attribute within the related condition's scope, cannot be a resource.</td><td><code>"order.line_items.quantity"</code></td></tr><tr><td><strong><code>operator</code></strong></td><td><span data-option="XJ81bAWrKRl1">String</span></td><td>true</td><td>The operator to be used on the field value to aggregate the results (check the <a href="/pages/TmykIt9I97R6vtsQTCzN">list of available operators</a>).</td><td><code>"sum"</code></td></tr><tr><td><strong><code>matcher</code></strong></td><td><span data-option="XJ81bAWrKRl1">String</span></td><td>true</td><td>The matcher to be used against the operator result and the provided value (check the <a href="/pages/5lnaIFYXLNMrvjiYzJH3#aggregations">list of available matchers)</a>.</td><td><code>"gteq"</code></td></tr><tr><td><strong><code>value</code></strong></td><td><span data-option="E0Cs57JSKyBG">Integer</span></td><td>true</td><td>The value to be compared against the operator result on the field value using the matcher.</td><td><code>2</code></td></tr></tbody></table>

{% hint style="info" %}
Multiple aggregations (on the same or different fields) are computed according to an `AND` logic.
{% endhint %}

{% hint style="warning" %}
The allowed aggregations `field` values are limited by the related condition's `field` value, meaning that the former must be within the scope defined by the latter (i.e. an attribute of one of the entities specified in the condition's field). For example, if the condition's field is `order.line_items.sku.code` you can aggregate over `order.line_items.quantity` but not over `order.customer.total_orders_count` because the customer entity is not within the scope set by the condition.
{% endhint %}

## Example

The following condition uses a couple of aggregations to check if an order contains at least three units in total of products tagged with any of a given set of tags and the cheapest one costs no less than a specific value

* The main matcher checks which line items of the order are associated with an SKU tagged with one of the two desired tags.
* The first aggregation's operator adds those line items' quantities and the related aggregation's matcher checks if the resulting sum is greater than or equal to 3.
* The second aggregation's operator identifies the cheapest of those line items' unit amount cents and the related aggregation's matcher checks if that cost exceeds the desired value.

<pre class="language-json"><code class="lang-json">"conditions": [
  {
    "field": "order.line_items.sku.tags.name",
    "matcher": "is_in",
    "value": [ "summer2022", "vipsale" ],
<strong>    "aggregations": [
</strong>      {
        "field": "order.line_items.quantity",
        "operator": "sum",
        "matcher": "gteq",
        "value": 3
      },
      {
        "field": "order.line_items.unit_amount_cents",
        "operator": "min",
        "matcher": "gt",
        "value": 5000
      }
    ]
  }
]
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.commercelayer.io/rules-engine/conditions/aggregations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
