# Aggregation

The selected items on which an action should apply 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 items on which to apply the action.

{% hint style="info" %}
Unlike the condition's `aggregations` key, which is an array of aggregation objects, an action's `aggregation` key is a single object (with the [same structure](/rules-engine/conditions/aggregations.md)).
{% endhint %}

{% hint style="warning" %}
The allowed aggregation `field` values are limited by the related action's `selector` 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 action's selector). For example, if the action's selector is `order.line_items` 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 selector.
{% endhint %}

## Example

The following action applies a 30% discount to the order's line items labeled as `discountable-items` by the related conditions only if the sum of their total amounts doesn't exceed 4500 cents.

<pre class="language-json"><code class="lang-json">"actions": [
  {
    "type": "percentage",
    "selector": "order.line_items",
    "groups": [ "discountable-items" ],
<strong>    "aggregation": {
</strong><strong>      "field": "order.line_items.total_amount_cents",
</strong><strong>      "operator": "sum",
</strong><strong>      "matcher": "lteq",
</strong><strong>      "value": 4500
</strong><strong>    },
</strong>    "value": 0.3
  }
]
</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/actions/aggregation.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.
