# 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: 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://developer.mediarithmics.io/schema/computed-fields.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.
