> For the complete documentation index, see [llms.txt](https://developer.mediarithmics.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.mediarithmics.io/schema/computed-fields.md).

# Computed fields

{% hint style="info" %}
This feature is in Alpha.
{% endhint %}

## Definition

A computed field is a dynamic field defined in the schema, linked to a script that is triggered for each new user activity or profile update and performs a computation of a function's result **on a regular basis**. It is used when the desired outcome cannot be achieved with existing directives or to simplify an OTQL query by replacing multiple directives with a single field.

Those fields can be used in segment definition to improve analysis and sharing, or in the ***Experiment*** feature to enhance control groups creation.

### Examples

Those examples are handled by computed fields:

* Retrieve the most recent event (e.g. last visit, last transaction, etc.)
* Get the total amount for a specific type of event (e.g. sum of transactions in the last 30 days, total expenditure on a particular category of product, etc.).
* Weighted sum (Affinity total, etc.)
* Compare multiple channels (e.g. to find the best channel for transactions).

This example should be handled by a standard directive:

* Get all users with at least a specific amount for one category

This example can't be handled by a computed field:

* Identify the top X% of a specific user group (e.g. the top 10% of buyers).

### **Usage Example**

Once live, a computed field behaves like a regular field, making it transparent to the user. Here's how you can use it in practice:

> Use Case: Identify Users Who Have Spent More Than 100€ on IT Products in the Last 3 Months

**With Standard Directives:**

```sql
// With standard directives
SELECT { id } FROM UserPoint
WHERE activity_events @ScoreSum(min : 100) {
    basket { 
        items @ScoreField(name:"price") @ScoreSum(result:"score_value") {
            category="IT" AND date >= "now-3M/M"
        }
    }
}
```

This query uses standard directives to sum the basket amounts for **IT** products over the last 3 months.

**With a Computed Field:**

```sql
//With a COmputed field "IT_amount_3months"
SELECT { id } FROM UserPoint WHERE IT_amount_3months >= 100
```

In this version, the computed field `IT_amount_3months` is used directly in the query. The field is calculated on a regular basis, meaning no computation is performed during query execution. This results gives faster query response times, as the value is precomputed and readily available.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developer.mediarithmics.io/schema/computed-fields.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
