# Breakdown

To perform a breakdown query on the return resource send a `POST` request to the `/returns/breakdown` endpoint specifying in the payload the [query keys](#query-keys) and [filter](/metrics-api-reference/resources/returns/filters.md) parameters like in the generic example below:

```json
{
  "breakdown": {
    "by": "...",
    "field": "...",
    "operator": "...",
    "condition": { ... },
    "sort": "...",
    "limit": ...,
    "breakdown": {
      "by": "...",
      "field": "...",
      "operator": "...",
      "condition": { ... },
      "sort": "...",
      "limit": ...
    }
  },
  "filter": { ... }
}
```

{% hint style="info" %}
Please find more information on how breakdown queries work [here](/metrics/getting-started/queries/breakdown.md).
{% endhint %}

### Query keys

These are the keys you need to set when performing a breakdown query:

<table><thead><tr><th>Key</th><th>Type</th><th data-type="checkbox">Required</th><th>Description</th><th>Values</th></tr></thead><tbody><tr><td><strong><code>by</code></strong></td><td>String</td><td>true</td><td>The field you want the results of the query aggragated by.</td><td>See the <a href="#nestable-breakdowns-and-by-values">related table</a> to check the full list of valid values for this key.</td></tr><tr><td><strong><code>field</code></strong></td><td>String</td><td>true</td><td>The field you want the metrics or statistics computed on.</td><td>See the <a href="#operators-and-field-values">related table</a> to check the full list of valid values for this key.</td></tr><tr><td><strong><code>operator</code></strong></td><td>String</td><td>true</td><td>The computing operator.</td><td>See the <a href="#operators-and-field-values">related table</a> to check the full list of valid operators based on the value you assigned to the <code>field</code> key.</td></tr><tr><td><strong><code>condition</code></strong></td><td>Object</td><td>false</td><td>An additional constraint to fine-tune the set of records shown in the response, applied to the computed results of the query. It is available for operators that return single numeric (float or integer) values.</td><td><p>One of:<br><code>"eq": ...</code></p><p><code>"ne": ...</code></p><p><code>"gt": ...</code></p><p><code>"gte": ...</code></p><p><code>"lt": ...</code></p><p><code>"lte": ...</code></p><p><code>"gt_lt": [...]</code></p><p><code>"gte_lte": [...]</code></p><p><code>"gte_lt": [...]</code></p><p><code>"gt_lte": [...]</code><br>(default is no condition).</p></td></tr><tr><td><strong><code>sort</code></strong></td><td>String</td><td>false</td><td>The way you want the results of the query to be sorted.</td><td>One of <code>asc</code> or <code>desc</code> (default is <code>desc</code>).</td></tr><tr><td><strong><code>limit</code></strong></td><td>Integer</td><td>false</td><td>The maximum number of records shown in the response.</td><td>Default is <code>10</code>, max is <code>100</code>.</td></tr><tr><td><strong><code>breakdown</code></strong></td><td>Object</td><td>false</td><td>The optional nested breakdown.</td><td>See the <a href="#nestable-breakdowns-and-by-values">related table</a> to check full list of fobidden values for the nested breakdown <code>by</code> key, based on the value you assigned to the parent breakdown <code>by</code> key.</td></tr></tbody></table>

### Operators and `field` values

These are the valid values you can specify for the `field` key of the breakdown query and the related valid operators, based on that key:

| Value                                          | Operators                                                                                                             |
| ---------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| **`customer.id`**                              | `value_count`                                                                                                         |
| **`customer.email`**                           | `value_count`                                                                                                         |
| **`customer.group_name`**                      | `value_count`                                                                                                         |
| **`destination_address.business`**             | `value_count`                                                                                                         |
| **`destination_address.city`**                 | `value_count`                                                                                                         |
| **`destination_address.country_code`**         | `value_count`                                                                                                         |
| **`destination_address.geocoded`**             | `value_count`                                                                                                         |
| **`destination_address.localized`**            | `value_count`                                                                                                         |
| **`destination_address.state_code`**           | `value_count`                                                                                                         |
| **`destination_address.zip_code`**             | `value_count`                                                                                                         |
| **`origin_address.business`**                  | `value_count`                                                                                                         |
| **`origin_address.city`**                      | `value_count`                                                                                                         |
| **`origin_address.country_code`**              | `value_count`                                                                                                         |
| **`origin_address.geocoded`**                  | `value_count`                                                                                                         |
| **`origin_address.localized`**                 | `value_count`                                                                                                         |
| **`origin_address.state_code`**                | `value_count`                                                                                                         |
| **`origin_address.zip_code`**                  | `value_count`                                                                                                         |
| **`market.id`**                                | <p><code>cardinality</code></p><p><code>value\_count</code></p>                                                       |
| **`market.number`**                            | <p><code>cardinality</code></p><p><code>value\_count</code></p>                                                       |
| **`return.number`**                            | `value_count`                                                                                                         |
| **`return.reference`**                         | `value_count`                                                                                                         |
| **`return.reference_origin`**                  | `value_count`                                                                                                         |
| **`return.status`**                            | `value_count`                                                                                                         |
| **`return.id`**                                | <p><code>cardinality</code></p><p><code>value\_count</code></p>                                                       |
| **`return.order_id`**                          | `value_count`                                                                                                         |
| **`return.skus_count`**                        | <p><code>avg</code></p><p><code>max</code></p><p><code>min</code></p><p><code>sum</code></p><p><code>stats</code></p> |
| **`return_line_items.quantity`**               | <p><code>avg</code></p><p><code>max</code></p><p><code>min</code></p><p><code>sum</code></p><p><code>stats</code></p> |
| **`return_line_items.line_item_id`**           | `value_count`                                                                                                         |
| **`return_line_items.line_item_name`**         | `value_count`                                                                                                         |
| **`return_line_items.line_item_code`**         | `value_count`                                                                                                         |
| **`return_line_items.line_item_item_type`**    | `value_count`                                                                                                         |
| **`return_line_items.line_item_total_amount`** | <p><code>avg</code></p><p><code>max</code></p><p><code>min</code></p><p><code>sum</code></p><p><code>stats</code></p> |
| **`return_line_items.line_item_tax_amount`**   | <p><code>avg</code></p><p><code>max</code></p><p><code>min</code></p><p><code>sum</code></p><p><code>stats</code></p> |
| **`return_line_items.line_item_tax_rate`**     | <p><code>avg</code></p><p><code>max</code></p><p><code>min</code></p><p><code>sum</code></p><p><code>stats</code></p> |
| **`stock_location.id`**                        | `value_count`                                                                                                         |
| **`stock_location.name`**                      | `value_count`                                                                                                         |
| **`stock_location.reference`**                 | `value_count`                                                                                                         |
| **`stock_location.reference_origin`**          | `value_count`                                                                                                         |

### Nestable breakdowns and `by` values

These are the valid values you can specify for the `by` key of the breakdown query and the related *forbidden* values for the `by` key of the nested breakdown, based on the `by` key of the parent breakdown (i.e. for each row of the table below the full list of the *valid* values for the `by` key of the nested breakdown is given by all the values in the "Value" column except the values in the row's "Forbidden nesting" cell):

| Value                                       | Forbidden nesting                                                                                                                                                                                                                                                         |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`customer.email`**                        |                                                                                                                                                                                                                                                                           |
| **`customer.group_name`**                   |                                                                                                                                                                                                                                                                           |
| **`destination_address.business`**          | <p><code>destination\_address.geocoded</code></p><p><code>destination\_address.localized</code></p><p><code>customer.*</code></p><p><code>origin\_address.*</code></p>                                                                                                    |
| **`destination_address.city`**              | <p><code>destination\_address.country\_code</code></p><p><code>destination\_address.state\_code</code></p><p><code>customer.*</code></p><p><code>origin\_address.*</code></p>                                                                                             |
| **`destination_address.country_code`**      | <p><code>customer.*</code></p><p><code>origin\_address.*</code></p>                                                                                                                                                                                                       |
| **`destination_address.geocoded`**          | <p><code>destination\_address.*</code></p><p><code>customer.*</code></p><p><code>origin\_address.\*</code></p>                                                                                                                                                            |
| **`destination_address.localized`**         | <p><code>destination\_address.*</code></p><p><code>customer.*</code></p><p><code>origin\_address.\*</code></p>                                                                                                                                                            |
| **`destination_address.state_code`**        | <p><code>destination\_address.country\_code</code></p><p><code>customer.*</code></p><p><code>origin\_address.*</code></p>                                                                                                                                                 |
| **`destination_address.zip_code`**          | <p><code>destination\_address.country\_code</code></p><p><code>destination\_address.city</code></p><p><code>destination\_address.geocoded</code></p><p><code>destination\_address.localized</code></p><p><code>customer.*</code></p><p><code>origin\_address.*</code></p> |
| **`origin_address.business`**               | <p><code>origin\_address.geocoded</code></p><p><code>origin\_address.localized</code></p><p><code>customer.*</code></p><p><code>destination\_address.*</code></p>                                                                                                         |
| **`origin_address.city`**                   | <p><code>origin\_address.country\_code</code></p><p><code>origin\_address.state\_code</code></p><p><code>customer.*</code></p><p><code>destination\_address.*</code></p>                                                                                                  |
| **`origin_address.country_code`**           | <p><code>customer.*</code></p><p><code>destrintation\_address.*</code></p>                                                                                                                                                                                                |
| **`origin_address.geocoded`**               | <p><code>destination\_address.*</code></p><p><code>customer.*</code></p><p><code>origin\_address.\*</code></p>                                                                                                                                                            |
| **`origin_address.localized`**              | <p><code>destination\_address.*</code></p><p><code>customer.*</code></p><p><code>origin\_address.\*</code></p>                                                                                                                                                            |
| **`origin_address.state_code`**             | <p><code>origin\_address.country\_code</code></p><p><code>customer.*</code></p><p><code>destination\_address.*</code></p>                                                                                                                                                 |
| **`origin_address.zip_code`**               | <p><code>origin\_address.country\_code</code></p><p><code>origin\_address.city</code></p><p><code>origin\_address.geocoded</code></p><p><code>origin\_address.localized</code></p><p><code>customer.*</code></p><p><code>destination\_address.*</code></p>                |
| **`market.id`**                             |                                                                                                                                                                                                                                                                           |
| **`market.name`**                           |                                                                                                                                                                                                                                                                           |
| **`market.number`**                         |                                                                                                                                                                                                                                                                           |
| **`return.reference`**                      |                                                                                                                                                                                                                                                                           |
| **`return.reference_origin`**               |                                                                                                                                                                                                                                                                           |
| **`return.status`**                         |                                                                                                                                                                                                                                                                           |
| **`return.currency_code`**                  |                                                                                                                                                                                                                                                                           |
| **`return_line_items.line_item_name`**      |                                                                                                                                                                                                                                                                           |
| **`return_line_items.line_item_code`**      |                                                                                                                                                                                                                                                                           |
| **`return_line_items.line_item_item_type`** |                                                                                                                                                                                                                                                                           |
| **`stock_location.name`**                   |                                                                                                                                                                                                                                                                           |
| **`stock_location.reference`**              |                                                                                                                                                                                                                                                                           |
| **`stock_location.reference_origin`**       |                                                                                                                                                                                                                                                                           |
| **`tags.id`**                               |                                                                                                                                                                                                                                                                           |
| **`tags.name`**                             |                                                                                                                                                                                                                                                                           |

{% hint style="info" %}
Remember also that you cannot group the nested breakdown by the same field by which you're already grouping the parent breakdown.
{% endhint %}


---

# 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/metrics-api-reference/resources/returns/breakdown.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.
